<?php
ini_set(display_errors,"0");
$db[dbname] = "pajak";
$db[user] = "nksimpro_baru";
$db[password] = "nksimpro_baru";
$db[host] = "localhost";
//$db[dbname] = "adhidb";
//$db[user] = "adhidb";
//$db[password] = "adhidb";
//$db[host] = "localhost";

function dbresult($sql){
	global $conn;
	global $db;
	if(!isset($conn)){
		while(list($key,$isi)=each($db)){
			$opt .= "$key=$isi ";
		}
		$conn = pg_connect($opt);
	}
	//echo $sql."<br>";
	if(!$result = pg_exec($conn, $sql)){
		echo "Proses ke database gagal";
		pg_errormessage($conn);

		echo $sql."<br>";
	}
	return $result;
}

//function dbresult($sql){
//	global $conn;
//	global $db;
//	global $muncul_error;
//	/*
//	if(!isset($conn)){
//		while(list($key,$isi)=each($db)){
//			$opt .= "$key=$isi ";
//		}
//		$conn = pg_connect($opt);
//	}
//	if(!$result = pg_exec($conn, $sql)){
//		if($muncul_error==""){
//		//echo "Proses ke database gagal<br>";
//		//echo "SQL : <br /><div style=\"border:1px solid #f00; padding:2px; margin:2px; \">".$sql."</div>";
//		pg_errormessage($conn);
//		}
//
//		//echo $sql."<br>";
//	}
//	return $result;
//	*/
//
//	// GANTI SESUAI SETTING
//
//	if(!isset($conn)){
//		$conn = pg_connect("host=127.0.0.1 port=5432 dbname=adhidb user=adhidb password=adhidb");
//	}
//	if(!$result = pg_exec($conn, $sql)){
//		echo "Proses ke database gagal";
//		pg_last_error($conn);
//		echo $sql."<br>";
//	}
//	return $result;
//}


$inp_cook[no_spk]="$cnospk";
#$inp_cook[divisi_kode]="$centitas";
$inp_cook[divisi_kode]="$cdivisi_kode";
$inp_cook[pusat_kode]="KP";
$inp_cook[user_update]="$cusername";
$inp_cook[username]="$cusername";
$inp_cook[divisi_update]="Divisi Mede";
$inp_cook[tgl_update]=date("Y-m-j");
$inp_cook[ip_update]=$REMOTE_ADDR;
$inp_cook[waktu_update]=date("H:i:s");
$inp_cook[kurs]=0;

$idr="RP";

if($cotoritas=="divisi"){
	$inp[filter_entitas]=" and divisi_kode='$cdivisi_kode'";
	$inp[filter_cmbpro]=" and divisi_kode='$cdivisi_kode'";
	$inp[filter_kontrak]=" and divisi_kode='$cdivisi_kode'";
	$inp[filter_data]=" and data_divisi_kode='$cdivisi_kode'";
}else{
	$inp[filter_entitas]=" and divisi_kode='$cdivisi_kode' and no_spk='$cnospk'";
	$inp[filter_kontrak]=" and divisi_kode='$cdivisi_kode' and kode_proyek='$cnospk'";
	$inp[filter_cmbpro]=" and divisi_kode='$cdivisi_kode'";
	$inp[filter_data]=" and data_divisi_kode='$cdivisi_kode' and data_no_spk='$cnospk'";
}
$folder_rap = "rap";
$folder_costcontrol = "costcontrol";
$folder_upload = "../upload/upload";
$folder_data = "data";
$folder_foto = "../upload/foto";
$folder_sertifikat = "../upload/sertifikat";
$folder_karya = "../upload/karya";
$folder_penghargaan = "../upload/penghargaan";
$folder_penugasan = "../upload/penugasan";

$curdir="/var/www/html/ais/";
$curdir="/home/adhidb/public_html/ais/";
$curdir_gambar="/var/www/html/simhk/img/";
$curdir_news="/var/www/html/simhk/news";
$NmFileLokasi="array_lokasi.js";
$NmFileUnitUsaha="array_unit_usaha.js";
$NmFileAccount="array_account";
$NmFileRekanan="array_rekanan";
$NmFileProyek="array_proyek";
$curdir_upload="/var/www/html/ais/finance/ledger/upload/";
$curdir_upload="/home/adhidb/public_html/ais/finance/ledger/upload/";
$virtual_dir="http://192.168.10.27/ais/";
$domain="http://192.168.0.112/";
$virtual_dir="/";
$domain="https://192.168.11.19/";
$domain="";
$path_cooki="/ais/";
$path_cooki="/finance/";
//$path_cooki="";
$analisa_dasar="AD";
//$tanda_account=cari_k();
//$tanda_laba=tanda_laba();

# FOR HRD
$HRD_Title_Page = "ADHI Human Resources";
# END

function check_data1($nilai,$nilai1){
	$selisih=$nilai-$nilai1;
	if(abs($selisih)>0.01){
		//echo  "<script>alert('Maaf RAP Dengan REKAPITULASI ANGGARAN Tidak Sama Mohon Cek Ulang Data atau Re-Calculate')</script>";
		$txtout.="<table border=0 width=98% align=center cellspacing=0 cellpadding=0><tr><td align=center><font color=red size=3><b>Maaf RAP Dengan REKAPITULASI ANGGARAN Tidak Sama, Mohon Cek Ulang Data atau Re-Calculate</b></td></tr></table>";
	}
	return $txtout;
}
function izin_app(){

}
function jumlah_tahap_rab($kode_tahap,$spk){
	global $kode,$set,$tgl_rab;
	$kode_tahap=trim($kode_tahap);
	if($kode_tahap<>""){
		$sql="select sum(tahap_total_rab) as \"jum\",sum(tahap_valas_rab) as \"val\" from tbl_tahap_rab where tahap_kode_induk_rab='".trim($kode_tahap)."' and no_spk='$spk' and tahap_tanggal_rab='$tgl_rab'";
		$result=dbresult($sql);
		$r=pg_fetch_array($result);//echo $sql."<br>";
		$r[jum]=$r[jum]==""?0:$r[jum];
		$r[val]=$r[val]==""?0:$r[val];
		$sql1="update tbl_tahap_rab set tahap_harga_satuan_rab='$r[jum]',tahap_total_rab=".$r[jum]." * tahap_volume_rab,tahap_harga_valas_rab='$r[val]',tahap_valas_rab=".$r[val]." * tahap_volume_rab where tahap_kode_rab='$kode_tahap' and no_spk='$spk' and tahap_tanggal_rab='$tgl_rab'";
#echo $sql1." jumlah_tahap_rab<br>";
		$resul1t=dbresult($sql1);

		$sql2="select * from tbl_tahap_rab where tahap_kode_rab='$kode_tahap' and no_spk='$spk' and tahap_tanggal_rab='$tgl_rab'";
		$result2=dbresult($sql2);//echo $sql2."<br>";
		$f=pg_fetch_array($result2);
		if(trim($f[tahap_kode_induk_rab])<>""){
			jumlah_tahap_rab(trim($f[tahap_kode_induk_rab]),$spk);
		}
	}
}

function tahap_bq($no_spk,$tahap_kode_bq,$tahap_tanggal_bq){
	$sql="select sum(tahap_total_bq) as total, sum(tahap_valas_bq) as valas from tbl_tahap_bq where tahap_kode_induk_bq='$tahap_kode_bq' and no_spk='$no_spk' and tahap_tanggal_bq='$tahap_tanggal_bq'";			
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
#echo $sql."<br>";

	$sql_="update tbl_tahap_bq set tahap_harga_satuan_bq=$r[total],tahap_total_bq=$r[total]*tahap_volume_bq,tahap_harga_valas_bq=$r[valas],tahap_valas_bq=$r[valas]*tahap_volume_bq where tahap_kode_bq='$tahap_kode_bq' and no_spk='$no_spk' and tahap_tanggal_bq='$tahap_tanggal_bq'";
#echo $sql_."<br>";
	dbresult($sql_);
	$tahap_kode_induk_bq=substr( $tahap_kode_bq,0,strrpos($tahap_kode_bq,"."));
	if($tahap_kode_induk_bq!=""){
		tahap_bq($no_spk,$tahap_kode_induk_bq,$tahap_tanggal_bq);
	}
}


function tahap_rab($no_spk,$tahap_kode_rab,$tahap_tanggal_rab){
	$sql="select sum(tahap_total_rab) as total, sum(tahap_valas_rab) as valas from tbl_tahap_rab where tahap_kode_induk_rab='$tahap_kode_rab' and no_spk='$no_spk' and tahap_tanggal_rab='$tahap_tanggal_rab'";			
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
#echo $sql."<br>";

	$sql_="update tbl_tahap_rab set tahap_harga_satuan_rab=$r[total],tahap_total_rab=$r[total]*tahap_volume_rab,tahap_harga_valas_rab=$r[valas],tahap_valas_rab=$r[valas]*tahap_volume_rab where tahap_kode_rab='$tahap_kode_rab' and no_spk='$no_spk' and tahap_tanggal_rab='$tahap_tanggal_rab'";
#echo $sql_."<br>";
	dbresult($sql_);
	$tahap_kode_induk_rab=substr( $tahap_kode_rab,0,strrpos($tahap_kode_rab,"."));
	if($tahap_kode_induk_rab!=""){
		tahap_rab($no_spk,$tahap_kode_induk_rab,$tahap_tanggal_rab);
	}
}


function jumlah_tahap_kendali_status($kode_tahap,$spk){
	global $kode,$set,$arr_tahap_status;
	global $tgl_rab,$tgl_rabe;
	if($tgl_rabe<>''){
		$tgl_rabe=$tgl_rabe;
	}else{
		$tgl_rabe=$tgl_rab;
	}
	$kode_tahap=trim($kode_tahap);
	//if(!$arr_tahap_status["$kode_tahap"]){
	if($kode_tahap<>""){
		$sql="select sum(tahap_total_kendali) as \"jum\",sum(tahap_valas_kendali) as \"val\" from tbl_tahap_kendali where tahap_kode_induk_kendali='".trim($kode_tahap)."' and no_spk='$spk' and tahap_tanggal_kendali='$tgl_rabe'";
		$result=dbresult($sql);
		$r=pg_fetch_array($result);//echo $sql."<br>";
		$r[jum]=$r[jum]==""?0:$r[jum];
		$r[val]=$r[val]==""?0:$r[val];
		$sql1="update tbl_tahap_kendali set tahap_harga_satuan_kendali='$r[jum]',tahap_total_kendali=".$r[jum]." * tahap_volume_kendali,tahap_harga_valas_kendali='$r[val]',tahap_valas_kendali=".$r[val]." * tahap_volume_kendali where tahap_kode_kendali='$kode_tahap' and no_spk='$spk' and tahap_tanggal_kendali='$tgl_rab'";
		$resul1t=dbresult($sql1);//echo $sql1." jumlah_tahap_rab<br>";
		//$arr_tahap_status["$kode_tahap"]=true;
	}
	//}
}

function array_volume_kendali($x_no_spk,$tgl_kendali){
	global $tgl_rab;
	$sql="select tahap_volume_kendali,tahap_kode_kendali from tbl_tahap_kendali where no_spk='$x_no_spk' and tahap_tanggal_kendali='$tgl_kendali'";
	$result=dbresult($sql);//echo $sql."<br>";
	while($r=pg_fetch_array($result)){
		$arr_volume["$r[tahap_kode_kendali]"]=$r[tahap_volume_kendali];
	}
	$arr_vol_akhir=$arr_volume;
	while(list($kode_x,$nial)=each($arr_volume)){
		$arr_kode=split("\.","$kode_x");	
		$hasil=count($arr_kode);
		$vol_total=1;
		for($i=$hasil-1;$i>=0;$i--){
			for($j=0;$j<=$i;$j++){
				$kode_y.="$arr_kode[$j].";
			}
			$kode_y=substr($kode_y,0,strlen($kode_y)-1);
			$vol_total=$vol_total*$arr_volume[$kode_y];
			$kode_y="";
		}
		$arr_vol_akhir[$kode_x]=$vol_total;
		$vol_total=1;
	}
	return $arr_vol_akhir;
}


function update_volume_kendali($kode_kendali,$x_no_spk,$tgl_kendali,$tgl_rabe=''){
	if($tgl_rabe<>''){
		$arr_vol=array_volume_kendali($x_no_spk,$tgl_rabe);
		$sql="select * from tbl_komposisi_kendali where no_spk='$x_no_spk' and tahap_tanggal_kendali='$tgl_rabe' and (tahap_kode_kendali like('$kode_kendali.%') or tahap_kode_kendali ='$kode_kendali')";//echo $sql."<br>";
		$sql="select tbl_komposisi_kendali.tahap_tanggal_kendali,detail_material_kode,kode_komposisi_kendali,tbl_komposisi_kendali.no_spk,komposisi_harga_satuan_kendali,tahap_kode_kendali,volume_komposisi,koefisien_komposisi,komposisi_koefisien_kendali,komposisi_volume_kendali from tbl_komposisi_kendali,tbl_induk_sd_kendali where tbl_komposisi_kendali.no_spk=tbl_induk_sd_kendali.no_spk and tbl_komposisi_kendali.kode_komposisi_kendali=tbl_induk_sd_kendali.kode_komposisi and tbl_komposisi_kendali.tahap_kode_kendali=tbl_induk_sd_kendali.kode_tahap_kendali and tbl_komposisi_kendali.tahap_tanggal_kendali=tbl_induk_sd_kendali.tahap_tanggal_kendali and tbl_komposisi_kendali.no_spk='$x_no_spk' and tbl_komposisi_kendali.tahap_tanggal_kendali='$tgl_rabe'";
		$result=dbresult($sql);//echo $sql."<br>";
		while($r=pg_fetch_array($result)){
			$vol_induk=is_numeric($r[volume_komposisi])?$r[volume_komposisi]:1;
			$koef_induk=is_numeric($r[koefisien_komposisi])?$r[koefisien_komposisi]:1;
			$vol_baru=$arr_vol["$r[tahap_kode_kendali]"]*$koef_induk *$r[komposisi_koefisien_kendali]*$vol_induk*$r[komposisi_volume_kendali];
			$harga_total=$vol_baru*$r[komposisi_harga_satuan_kendali];
			$sql_update="update tbl_komposisi_kendali set komposisi_volume_total_kendali='$vol_baru' where detail_material_kode='$r[detail_material_kode]' and tahap_kode_kendali='$r[tahap_kode_kendali]' and no_spk='$r[no_spk]' and kode_komposisi_kendali='$r[kode_komposisi_kendali]' and tahap_tanggal_kendali='$tgl_kendali'";
			dbresult($sql_update);//echo $sql_update."<br>";
		}
	}else{
		$arr_vol=array_volume_kendali($x_no_spk,$tgl_kendali);
		$sql="select * from tbl_komposisi_kendali where no_spk='$x_no_spk' and tahap_tanggal_kendali='$tgl_kendali' and (tahap_kode_kendali like('$kode_kendali.%') or tahap_kode_kendali ='$kode_kendali')";//echo $sql."<br>";
		$sql="select tbl_komposisi_kendali.tahap_tanggal_kendali,detail_material_kode,kode_komposisi_kendali,tbl_komposisi_kendali.no_spk,komposisi_harga_satuan_kendali,tahap_kode_kendali,volume_komposisi,koefisien_komposisi,komposisi_koefisien_kendali,komposisi_volume_kendali from tbl_komposisi_kendali,tbl_induk_sd_kendali where tbl_komposisi_kendali.no_spk=tbl_induk_sd_kendali.no_spk and tbl_komposisi_kendali.kode_komposisi_kendali=tbl_induk_sd_kendali.kode_komposisi and tbl_komposisi_kendali.tahap_kode_kendali=tbl_induk_sd_kendali.kode_tahap_kendali and tbl_komposisi_kendali.tahap_tanggal_kendali=tbl_induk_sd_kendali.tahap_tanggal_kendali and tbl_komposisi_kendali.no_spk='$x_no_spk' and tbl_komposisi_kendali.tahap_tanggal_kendali='$tgl_kendali'";
		$result=dbresult($sql);//echo $sql."<br>";
		while($r=pg_fetch_array($result)){
			$vol_induk=is_numeric($r[volume_komposisi])?$r[volume_komposisi]:1;
			$koef_induk=is_numeric($r[koefisien_komposisi])?$r[koefisien_komposisi]:1;
			$vol_baru=$arr_vol["$r[tahap_kode_kendali]"]*$koef_induk *$r[komposisi_koefisien_kendali]*$vol_induk*$r[komposisi_volume_kendali];
			$harga_total=$vol_baru*$r[komposisi_harga_satuan_kendali];
			$sql_update="update tbl_komposisi_kendali set komposisi_volume_total_kendali='$vol_baru' where detail_material_kode='$r[detail_material_kode]' and tahap_kode_kendali='$r[tahap_kode_kendali]' and no_spk='$r[no_spk]' and kode_komposisi_kendali='$r[kode_komposisi_kendali]' and tahap_tanggal_kendali='$r[tahap_tanggal_kendali]'";
			dbresult($sql_update);//echo $sql_update."<br>";
		}
	}
}


function jumlah_tahap_kendali($kode_tahap,$spk){
	global $kode,$set,$tgl_rab;
	if(trim($kode_tahap)<>""){
		$sql="select sum(tahap_total_kendali) as \"jum\",sum(tahap_valas_kendali) as \"val\" from tbl_tahap_kendali where tahap_kode_induk_kendali='$kode_tahap' and no_spk='$spk'  and tahap_tanggal_kendali='$tgl_rab'";
		$result=dbresult($sql);
		$r=pg_fetch_array($result);//echo $sql." test<br>";
		$r[jum]=$r[jum]==""?0:$r[jum];
		$r[val]=$r[val]==""?0:$r[val];
		$sql1="update tbl_tahap_kendali set tahap_harga_satuan_kendali='$r[jum]',tahap_total_kendali=".$r[jum]." * tahap_volume_kendali,tahap_harga_valas_kendali='$r[val]',tahap_valas_kendali=".$r[val]." * tahap_volume_kendali where tahap_kode_kendali='$kode_tahap' and no_spk='$spk'  and tahap_tanggal_kendali='$tgl_rab'";
		$resul1t=dbresult($sql1);//echo $sql."<br>";

		$sql2="select * from tbl_tahap_kendali where tahap_kode_kendali='$kode_tahap' and no_spk='$spk'  and tahap_tanggal_kendali='$tgl_rab'";
		$result2=dbresult($sql2);
		$f=pg_fetch_array($result2);
		if(trim($f[tahap_kode_induk_kendali])<>""){
			jumlah_tahap_kendali(trim($f[tahap_kode_induk_kendali]),$spk);
		}
	}
}


function update_hs_kendali($kode,$nospk,$hs_rab=""){
	global $tgl_rab;
	if($hs_rab==''){
		$sqlgrpk="select sum(total_harga_satuan) as total from tbl_induk_sd_kendali where kode_tahap_kendali='$kode' and no_spk='$nospk' and kode_komposisi like 'K%' and tahap_tanggal_kendali='$tgl_rab'";
		$resgrpk=dbresult($sqlgrpk);//echo $sqlgrpk;
		$k=pg_fetch_array($resgrpk);
		$sqlsumx="select sum(komposisi_total_kendali) as \"total\" from qry_komposisi_kendali where tahap_kode_kendali='$kode' and no_spk='$nospk' and kode_komposisi_kendali='X' and tahap_tanggal_kendali='$tgl_rab'";
		$resumx=dbresult($sqlsumx);//echo $sqlsumx;
		$s=pg_fetch_array($resumx);
		$o=$k[total]+$s[total];
		$n[satuan]=$o;
	}else{
		$n[satuan]=$hs_rab;
	}
	if($n[satuan]<>""){
		$sql1="update tbl_tahap_kendali set tahap_harga_satuan_kendali='$n[satuan]',tahap_total_kendali=".$n[satuan]."* tahap_volume_kendali where tahap_kode_kendali='$kode' and no_spk='$nospk'  and tahap_tanggal_kendali='$tgl_rab'";
		$result1=dbresult($sql1);
		$kdi=substr($kode,0,strrpos($kode,"."));
		jumlah_tahap_kendali($kdi,$nospk);
	}
	if($n[satuan]==""){
		$n[satuan]=0;
		$sql1="update tbl_tahap_kendali set tahap_harga_satuan_kendali='$n[satuan]',tahap_total_kendali=".$n[satuan]."* tahap_volume_kendali where tahap_kode_kendali='$kode' and no_spk='$nospk'  and tahap_tanggal_kendali='$tgl_rab'";
		$result1=dbresult($sql1);
		$kdi=substr($kode,0,strrpos($kode,"."));
		jumlah_tahap_kendali($kdi,$nospk);
	}
}

function cari_volume_recal_kendali($kode_x,$no_spk,$tgl_rab,$arr_tahap_kendali){
	//$arr_tahap_rab=arr_tahap_rab($no_spk);
	$arr_kode=split("\.","$kode_x");	
	$hasil=count($arr_kode);
	$vol_total=1;
	for($i=$hasil-1;$i>=0;$i--){
		for($j=0;$j<=$i;$j++){
			$kode_y.="$arr_kode[$j].";
		}
		$kode_y=substr($kode_y,0,strlen($kode_y)-1);
		//echo $kode_y."<br>";
		$arr=split("\t",$arr_tahap_kendali[$kode_y]);
		$vol_total=$vol_total*$arr[0];
		//echo "$vol_total=$vol_total*$arr[0] -- $kode_y<br>";
		$kode_y="";
	}
	return $vol_total;
}



function cari_volume_kendali($kode_x,$no_spk,$tgl_kendali){
	$arr_kode=split("\.","$kode_x");	
	$hasil=count($arr_kode);
	$vol_total=1;
	for($i=$hasil-1;$i>=0;$i--){
		for($j=0;$j<=$i;$j++){
			$kode_y.="$arr_kode[$j].";
		}
		$kode_y=substr($kode_y,0,strlen($kode_y)-1);
		//echo $kode_y."<br>";
		$sql="select sum(tahap_volume_kendali) as tahap_volume_kendali from tbl_tahap_kendali where no_spk='$no_spk' and tahap_kode_kendali='$kode_y' and tahap_tanggal_kendali='$tgl_kendali'";
		$result=dbresult($sql);//echo $sql."<br>";
		while($r=pg_fetch_array($result)){
			$vol_total=$vol_total*$r[tahap_volume_kendali];
			//echo "$vol_total=$vol_total*$r[tahap_volume_rab] <br>";
		}
		$kode_y="";
	}
	return $vol_total;
}


function cari_volume_rab($kode_x,$no_spk){
	$arr_kode=split("\.","$kode_x");	
	$hasil=count($arr_kode);
	$vol_total=1;
	for($i=$hasil-1;$i>=0;$i--){
		for($j=0;$j<=$i;$j++){
			$kode_y.="$arr_kode[$j].";
		}
		$kode_y=substr($kode_y,0,strlen($kode_y)-1);
		//echo $kode_y."<br>";
		$sql="select sum(tahap_volume_rab) as tahap_volume_rab from tbl_tahap_rab where no_spk='$no_spk' and tahap_kode_rab='$kode_y'";
		$result=dbresult($sql);//echo $sql."<br>";
		while($r=pg_fetch_array($result)){
			$vol_total=$vol_total*$r[tahap_volume_rab];
			//echo "$vol_total=$vol_total*$r[tahap_volume_rab] -- $kode_y<br>";
		}
		$kode_y="";
	}
	return $vol_total;
}


function insert_tahap_rab($spk,$cnama,$tgl_update,$cdivisi_name,$ip_update){
	$jam=date('H:i:s');
	$sql_add="insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('1','Biaya Langsung','Ls','$spk','1','0','0','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2','Biaya Tak Langsung','Ls','$spk','1','0','0','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1','Biaya Umum Lapangan','Ls','$spk','1','0','0','2','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.1','Biaya Pegawai','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.2','Biaya Umum','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.3','Biaya Pemasaran','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.4','Biaya Jamuan Relasi','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.5','Biaya Laboratorium','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.6','Biaya Biaya Keselamatan Kerja','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.7','Biaya Kendaraan','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.8','Biaya Perijinan/Persiapan','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.1.9','Biaya Asuransi & Referensi Proyek','Ls','$spk','1','0','0','2.1','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.2','Biaya Bunga Operasional','Ls','$spk','1','0','0','2','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.2.1','Biaya Bunga Bank','Ls','$spk','1','0','0','2.2','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.2.2','Biaya Bunga Bank Non Kredit Bank','Ls','$spk','1','0','0','2.2','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.2.3','Biaya Provisi & Materai','Ls','$spk','1','0','0','2.2','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');
	insert into tbl_tahap_rab(tahap_kode_rab,tahap_nama_rab,tahap_satuan_rab,no_spk,tahap_volume_rab,tahap_harga_satuan_rab,tahap_total_rab,tahap_kode_induk_rab,user_update,tgl_update,ip_update,divisi_update,waktu_update,tahap_tanggal_rab) values('2.2.4','Biaya Transfer & Administrasi','Ls','$spk','1','0','0','2.2','$cnama','$tgl_update','$ip_update','$cdivisi_name','$jam','$tgl_update');";
	dbresult($sql_add);
}

function check_komposisi($kode,$spk){
	global $sql_c_divisi,$sql_c_spk,$sql_c_propinsi;
	/*
	   $sql="select * from tbl_komposisi_rab where tahap_kode_rab='$kode' and no_spk='$spk'";
	//echo $sql."<br>";
	$res=dbresult($sql);
	if(pg_num_rows($res)>0){
	return true;
	}else{
	return false;
	}*/
}

function update_volume_bq($x_no_spk){
	global $tgl_rab;
	$sql="select * from tbl_komposisi_bq where no_spk='$x_no_spk' and tahap_tanggal_bq='$tgl_rab'";
	$result=dbresult($sql);
	while($r=pg_fetch_array($result)){
		$sql_induk="select * from tbl_induk_sd_bq where no_spk='$x_no_spk' and kode_tahap_bq='$r[tahap_kode_bq]' and kode_komposisi='$r[kode_komposisi_bq]' and tahap_tanggal_bq='$tgl_rab' order by kode_komposisi";
		$res_induk=dbresult($sql_induk);
		if(pg_num_rows($res_induk)>0){
			$r_induk=pg_fetch_array($res_induk);
			$vol_induk=$r_induk[volume_komposisi];
			$koef_induk=$r_induk[koefisien_komposisi];
		}else{
			$vol_induk=1;
			$koef_induk=1;
		}
		//$vol_baru=cari_volume_bq($r[tahap_kode_bq],$r[no_spk])*$koef_induk *$r[komposisi_koefisien_bq]*$vol_induk*$r[komposisi_volume_bq];cari_volume_bq($r[tahap_kode_bq],$r[no_spk])*$r[komposisi_volume_bq]
		$vol_baru=cari_volume_bq($r[tahap_kode_bq],$r[no_spk])*$koef_induk *$r[komposisi_koefisien_bq]*$vol_induk*$r[komposisi_volume_bq];
		$harga_total=$vol_baru*$r[komposisi_harga_satuan_bq];
		$valas_total=$vol_baru*$r[komposisi_valas_satuan_bq];

		$sql_update="update tbl_komposisi_bq set komposisi_volume_total_bq='$vol_baru' where detail_material_kode='$r[detail_material_kode]' and tahap_kode_bq='$r[tahap_kode_bq]' and no_spk='$r[no_spk]' and kode_komposisi_bq='$r[kode_komposisi_bq]' and tahap_tanggal_bq='$tgl_rab'";
		dbresult($sql_update);//echo $sql_update."<br>";
	}
}


function jumlah_tahap_bq($kode_tahap,$spk){
	global $kode,$set,$tgl_rab;
	if(trim($kode_tahap)<>""){
		$sql="select sum(tahap_total_bq) as \"jum\",sum(tahap_valas_bq) as \"val\" from tbl_tahap_bq where tahap_kode_induk_bq='$kode_tahap' and no_spk='$spk' and tahap_tanggal_bq='$tgl_rab'";
		$result=dbresult($sql);
		$r=pg_fetch_array($result);//echo $sql."<br>";
		$r[jum]=$r[jum]==""?0:$r[jum];
		$r[val]=$r[val]==""?0:$r[val];
		$sql1="update tbl_tahap_bq set tahap_harga_satuan_bq='$r[jum]',tahap_total_bq=".$r[jum]."* tahap_volume_bq* markup,tahap_harga_valas_bq='$r[val]',tahap_valas_bq=".$r[val]."* tahap_volume_bq* markup where tahap_kode_bq='$kode_tahap' and no_spk='$spk' and tahap_tanggal_bq='$tgl_rab'";
		$resul1t=dbresult($sql1);
		//echo $sql1."<br>";

		$sql2="select * from tbl_tahap_bq where tahap_kode_bq='$kode_tahap' and no_spk='$spk' and tahap_tanggal_bq='$tgl_rab'";
		$result2=dbresult($sql2);
		$f=pg_fetch_array($result2);
		if(trim($f[tahap_kode_induk_bq])<>""){
			jumlah_tahap_bq(trim($f[tahap_kode_induk_bq]),$spk);
		}
	}
}


function cari_volume_bq($kode_x,$no_spk){
	global $tgl_rab;
	$arr_kode=split("\.","$kode_x");	
	$hasil=count($arr_kode);
	$vol_total=1;
	for($i=$hasil-1;$i>=0;$i--){
		for($j=0;$j<=$i;$j++){
			$kode_y.="$arr_kode[$j].";
		}
		$kode_y=substr($kode_y,0,strlen($kode_y)-1);
		//echo $kode_y."<br>";
		$sql="select sum(tahap_volume_bq) as tahap_volume_bq from tbl_tahap_bq where no_spk='$no_spk' and tahap_kode_bq='$kode_y' and tahap_tanggal_bq='$tgl_rab'";
		$result=dbresult($sql);//echo $sql;
		while($r=pg_fetch_array($result)){
			$vol_total=$vol_total*$r[tahap_volume_bq];
			//echo "$vol_total=$vol_total*$r[tahap_volume_rab] <br>";
		}
		$kode_y="";
	}
	return $vol_total;
}

function update_hs_bq($kode,$nospk,$kdk){
	global $tgl_rab;
	global $cdiv_kode;
	$sql="select sum(komposisi_total_bq) as \"satuan\",sum(komposisi_valas_bq) as \"valas\" from tbl_komposisi_bq where tahap_kode_bq='$kode' and no_spk='$nospk' and tahap_tanggal_bq='$tgl_rab' and kode_komposisi_bq='$kdk'";
	$result=dbresult($sql);//echo "$sql<br>";
	$n=pg_fetch_array($result);
	$n[satuan]=$n[satuan]==""?0:$n[satuan];	
	$n[valas]=$n[valas]==""?0:$n[valas];	
	$sql="update tbl_induk_sd_bq set harga_satuan=$n[satuan],harga_valas=$n[valas],total_harga_satuan=$n[satuan]*volume_komposisi,total_harga_valas=$n[valas]*volume_komposisi where kode_tahap_bq='$kode' and no_spk='$nospk' and tahap_tanggal_bq='$tgl_rab' and kode_komposisi='$kdk'";
	dbresult($sql);

	$sql="select sum(total_harga_satuan) as \"satuan\",sum(total_harga_valas) as \"valas\" from tbl_induk_sd_bq where kode_tahap_bq='$kode' and no_spk='$nospk' and tahap_tanggal_bq='$tgl_rab'";
	$result=dbresult($sql);#echo "$sql<br>";
	$n=pg_fetch_array($result);
	$n[satuan]=$n[satuan]==""?0:$n[satuan];	
	$n[valas]=$n[valas]==""?0:$n[valas];	
	$sql1="update tbl_tahap_bq set tahap_harga_satuan_bq='$n[satuan]',tahap_total_bq=".$n[satuan]."* tahap_volume_bq* markup,tahap_harga_valas_bq='$n[valas]',tahap_valas_bq=".$n[valas]."* tahap_volume_bq* markup where tahap_kode_bq='$kode' and no_spk='$nospk' and tahap_tanggal_bq='$tgl_rab'";
	$result1=dbresult($sql1);#echo $sql1."<br>";

	$kdi=substr($kode,0,strrpos($kode,"."));
	jumlah_tahap_bq($kdi,$nospk);
#	echo $sql1."<br>";
}
function update_hs($kode,$nospk,$hs_rab="",$vls_rab=""){
	global $tgl_rab;
	if($hs_rab=='' and $vls_rab==''){
		$sqlgrpk="select sum(total_harga_satuan) as total,sum(total_harga_valas) as valas from tbl_induk_sd_rab where kode_tahap_rab='$kode' and no_spk='$nospk' and kode_komposisi like 'K%'  and tahap_tanggal_rab='$tgl_rab'";
		$resgrpk=dbresult($sqlgrpk);//echo $sqlgrpk;
		$k=pg_fetch_array($resgrpk);
		$sqlsumx="select sum(komposisi_total_rab) as \"total\",sum(komposisi_valas_rab) as \"valas\" from qry_komposisi where tahap_kode_rab='$kode' and no_spk='$nospk' and kode_komposisi_rab='X'  and tahap_tanggal_rab='$tgl_rab'";
		$resumx=dbresult($sqlsumx);//echo $sqlsumx;
		$s=pg_fetch_array($resumx);
		$o=$k[total]+$s[total];
		$v=$k[valas]+$s[valas];
		$n[satuan]=$o==""?0:$o;
		$n[valas]=$v==""?0:$v;
	}else{
		$n[satuan]=$hs_rab==""?0:$hs_rab;
		$n[valas]=$vls_rab==""?0:$vls_rab;
	}
#echo "$n[satuan]=$hs_rab -- $kode<br>";

	$sql1="update tbl_tahap_rab set tahap_harga_satuan_rab='$n[satuan]',tahap_total_rab=".$n[satuan]." * tahap_volume_rab,tahap_harga_valas_rab='$n[valas]',tahap_valas_rab=".$n[valas]." * tahap_volume_rab where tahap_kode_rab='$kode' and no_spk='$nospk'  and tahap_tanggal_rab='$tgl_rab'";
	$result1=dbresult($sql1);
	$kdi=substr($kode,0,strrpos($kode,"."));
#	echo $sql1."<br>";
	jumlah_tahap_rab($kdi,$nospk);
}


function cari_volume_recal($kode_x,$no_spk,$arr_tahap_rab){
	//$arr_tahap_rab=arr_tahap_rab($no_spk);
	$arr_kode=split("\.","$kode_x");
#echo "<pre>";
#print_r($arr_kode);
#echo "</pre>";
	$hasil=count($arr_kode);
	$vol_total=1;
	for($i=$hasil-1;$i>=0;$i--){
		for($j=0;$j<=$i;$j++){
			$kode_y.="$arr_kode[$j].";
		}
		$kode_y=substr($kode_y,0,strlen($kode_y)-1);
		//echo $kode_y."<br>";
		$arr=split("\t",$arr_tahap_rab[$kode_y]);
		$vol_total=$vol_total*$arr[0];
#	echo "$vol_total=$vol_total*$arr[0] -- $kode_y<br>";
		$kode_y="";
	}
	//		echo "$vol_total <--------- <br>";
	return $vol_total;
}


function arr_check_komposisi_kendali($spk,$tgl_rab){
	global $sql_c_divisi,$sql_c_spk,$sql_c_propinsi;
	$sql="select tahap_kode_kendali from tbl_komposisi_kendali where no_spk='$spk' and tahap_tanggal_kendali='$tgl_rab' group by tahap_kode_kendali ";
	//echo $sql."<br>";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_chk["$r[tahap_kode_kendali]"]=true;
	}
	return $arr_chk;
}

function arr_tahap_kendali($nospk,$tgl_rab){
	$sql="select * from tbl_tahap_kendali where no_spk='$nospk' and tahap_tanggal_kendali='$tgl_rab' order by length(tahap_kode_kendali),tahap_kode_kendali";
	$res=dbresult($sql);//echo "$sql<br>";
	while($r=pg_fetch_array($res)){
		$arr_x["$r[tahap_kode_kendali]"]=$r[tahap_volume_kendali]."\t".$r[tahap_harga_satuan_kendali]."\t".$r[tahap_total_kendali]."\t".$r[tahap_kode_induk_kendali]."\t".$r[tahap_kode_kendali]."\t".$r[tahap_tanggal_kendali]."\t".$r[tahap_nama_kendali]."\t".$r[tahap_satuan_kendali]."\t";
	}
	return $arr_x;
}


function arr_tahap_rab($nospk){
	global $tgl_rab;
	$sql="select * from tbl_tahap_rab where no_spk='$nospk' and tahap_tanggal_rab='$tgl_rab' order by length(tahap_kode_rab),tahap_kode_rab";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_x["$r[tahap_kode_rab]"]=$r[tahap_volume_rab]."\t".$r[tahap_harga_satuan_rab]."\t".$r[tahap_total_rab]."\t".$nilai_k."\t".$r[tahap_kode_induk_rab]."\t";
	}
	return $arr_x;
}

function arr_tahap_bq($nospk){
	global $tgl_rab;
	$sql="select * from tbl_tahap_bq where no_spk='$nospk' and tahap_tanggal_bq='$tgl_rab' order by length(tahap_kode_bq),tahap_kode_bq";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_x["$r[tahap_kode_bq]"]=$r[tahap_volume_bq]."\t".$r[tahap_harga_satuan_bq]."\t".$r[tahap_total_bq]."\t".$nilai_k."\t".$r[tahap_kode_induk_bq]."\t";
	}
	return $arr_x;
}


function jumlah_tahap_status($kode_tahap,$spk){
	global $kode,$set,$arr_tahap_status,$tgl_rab;
	$kode_tahap=trim($kode_tahap);
	//if(!$arr_tahap_status["$kode_tahap"]){
	if($kode_tahap<>""){
		$sql="select sum(tahap_total_rab) as \"jum\",sum(tahap_valas_rab) as \"val\" from tbl_tahap_rab where tahap_kode_induk_rab='".trim($kode_tahap)."' and no_spk='$spk'  and tahap_tanggal_rab='$tgl_rab'";
		$result=dbresult($sql);
		$r=pg_fetch_array($result);//echo $sql."<br>";
		$r[jum]=$r[jum]==""?0:$r[jum];
		$r[val]=$r[val]==""?0:$r[val];
		$sql1="update tbl_tahap_rab set tahap_harga_satuan_rab='$r[jum]',tahap_total_rab=".$r[jum]." * tahap_volume_rab,tahap_harga_valas_rab='$r[val]',tahap_valas_rab=".$r[val]." * tahap_volume_rab where tahap_kode_rab='$kode_tahap' and no_spk='$spk'  and tahap_tanggal_rab='$tgl_rab'";
		$resul1t=dbresult($sql1);//echo $sql1." jumlah_tahap_rab<br>";
		//$arr_tahap_status["$kode_tahap"]=true;
	}
	//}
}


function arr_check_komposisi_rab($spk){
	global $sql_c_divisi,$sql_c_spk,$sql_c_propinsi;
	$sql="select tahap_kode_rab from tbl_komposisi_rab where no_spk='$spk' group by tahap_kode_rab ";
	//echo $sql."<br>";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_chk["$r[tahap_kode_rab]"]=true;
	}
	return $arr_chk;
}


function arr_check_komposisi_bq($spk){
	global $sql_c_divisi,$sql_c_spk,$sql_c_propinsi;
	$sql="select tahap_kode_bq from tbl_komposisi_bq where no_spk='$spk' group by tahap_kode_bq ";
	//echo $sql."<br>";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_chk["$r[tahap_kode_bq]"]=true;
	}
	return $arr_chk;
}


function arr_check_komposisi($spk){
	global $sql_c_divisi,$sql_c_spk,$sql_c_propinsi;
	$sql="select tahap_kode_rab from tbl_komposisi_rab where no_spk='$spk' group by tahap_kode_rab ";
	//echo $sql."<br>";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_chk["$r[tahap_kode_rab]"]=true;
	}
	return $arr_chk;
}


function cari_k(){
	$sql="select acc_kode from tbl_fn_account where (acc_kode like '1%' or acc_kode like '5%' or acc_kode like '6%' or acc_kode like '8%' or acc_kode like '9%') group by acc_kode order by acc_kode";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$kode_acc.=",".$r[acc_kode];
	}
	return $kode_acc;
}

function tanda_lb($panjang=5){
	$sql="select substr(acc_kode,1,$panjang) as acc_kode from tbl_fn_account where acc_jenis='1' and level='5' group by substr(acc_kode,1,$panjang) order by acc_kode";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$kode_acc.=$r[acc_kode].",";
	}
	$kode_acc="'".substr($kode_acc,0,strlen($kode_acc)-1)."'";
	return $kode_acc;
}

function tanda_laba($panjang=2){
	$sql="select substr(acc_kode,1,$panjang) as acc_kode from tbl_fn_account where acc_jenis='1' and level='$panjang' group by substr(acc_kode,1,$panjang) order by acc_kode";
	//$sql="select substr(acc_kode,1,3) as acc_kode from tbl_fn_account where (acc_kode like '1%' or acc_kode like '5%' or acc_kode like '6%' or acc_kode like '8%' or acc_kode like '9%') group by substr(acc_kode,1,3) order by acc_kode";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$kode_acc.=",".$r[acc_kode];
	}
	$kode_acc="'".substr($kode_acc,1,strlen($kode_acc)-1)."'";
	return $kode_acc;
}

function var_kirim($inp_par,$ha_nama=""){
	if(is_array($inp_par)){
		while(list($nama,$nilai)=each($inp_par)){
			if(strpos(",$ha_nama,","$nama,")==0){
				$var_post.="&inp_par[$nama]=".rawurlencode($nilai)."";
#echo $nilai."<br>";
			}
		}
	}

#echo "<pre>";
#print_r($inp_par);
#echo "</pre>";

#	echo "$ha_nama,$var_post <br>";
	return $var_post;
}
function var_kirim2($inp_par,$ha_nama=""){
	if(is_array($inp_par)){
		while(list($nama,$nilai)=each($inp_par)){
			if(strpos(",$ha_nama,","$nama,")==0){
				$var_post.="&inp[$nama]=".rawurlencode($nilai)."";
#echo $nilai."<br>";
			}
		}
	}

#echo "<pre>";
#print_r($inp_par);
#echo "</pre>";

#	echo "$ha_nama,$var_post <br>";
	return $var_post;
}

function cek_nospk($nospk,$id=""){
	if($id!=""){
		$sqladd="and no_spk!='$id'";
	}
	$sql="select no_spk from tbl_proyek where no_spk='$nospk' $sqladd";
	$res=dbresult($sql);
	$jml=pg_num_rows($res);
	if($jml>0){
		return true;
	}else{
		return false;
	}
}

function inp_kirim($inp,$ha_nama=""){
	if(is_array($inp)){
		while(list($nama,$nilai)=each($inp)){
			if(strpos(",$ha_nama,","$nama,")==0){
				$var_post.="&inp[$nama]=$nilai";
			}
		}
	}
	//echo "$ha_nama,$var_post <br>";
	return $var_post;
}

$fungsi_pindah="<script>
function loncat(targ,selObj,restore){ //v3.0
	//alert(selObj.options[selObj.selectedIndex].value);
	window.location=selObj.options[selObj.selectedIndex].value;
	// if (restore) selObj.selectedIndex=0;
}
</script>";

$arr_keluaran["jenis"][""]="";
$arr_keluaran["jenis"][1]="Export";
$arr_keluaran["jenis"][2]="Penyerahan Dalam Negeri Dengan Faktur Pajak";
$arr_keluaran["detail"][""]="";
$arr_keluaran["detail"][1]="Kepada Pihak yang Bukan Pemungut PPN";
$arr_keluaran["detail"][2]="Kepada Pemungut Bendaharawan";
$arr_keluaran["detail"][3]="Kepada Pemungut Selain Bendaharawan";
$arr_keluaran["detail"][4]="DPP Nilai Lain";
$arr_keluaran["detail"][5]="Deemed Pajak Masukan";
$arr_keluaran["detail"][6]="Penyerahan Lainnya";
$arr_keluaran["detail"][7]="Penyerahan yang PPN-nya Tidak Dipungut";
$arr_keluaran["detail"][8]="Penyerahan yang PPN-nya Dibebaskan";
$arr_keluaran["detail"][9]="Penyerahan Aktiva (Pasal 16D UU PPN)";
$arr_keluaran["dokumen"][""]="";
$arr_keluaran["dokumen"][1]="Faktur Pajak Standar";
$arr_keluaran["dokumen"][2]="Nota Retur";
$arr_keluaran["dokumen"][3]="Dokumen Yang Diperlakukan sebagai FP Standar";
$arr_keluaran["dokumen"][4]="Faktur Batal";
$arr_keluaran["dokumen"][5]="Faktur Pajak Pengganti";
$arr_keluaran["diganti"][""]="";
$arr_keluaran["diganti"][1]="Faktur Pajak Pengganti";
$arr_keluaran["diganti"][2]="Dokumen Yang Diperlakukan sebagai FP Standar";

$arr_masukan["jenis"][""]="";
$arr_masukan["jenis"][1]="Impor BKP dan Pemanfaatan JKP/BKP Tidak Berwujud dari Luar Daerah Pabean";
$arr_masukan["jenis"][2]="Perolehan BKP/JKP Dari Dalam Negeri";
$arr_masukan["jenis"][3]="Pajak Masukan Yang Tidak Dapat Dikreditkan dan/atau Pajak Masukan dan PPnBM yang atas impor atau perolehannya mendapat fasilitas";

$arr_masukan["status"][""][""]="";
$arr_masukan["status"][1][""]="";
$arr_masukan["status"][1][1]="BKP";
$arr_masukan["status"][1][2]="BKP";
$arr_masukan["status"][2][""]="";
$arr_masukan["status"][2][1]="Kepada Pihak yang Bukan Pemungut PPN";
$arr_masukan["status"][2][2]="Kepada Pemungut Bendaharawan";
$arr_masukan["status"][2][3]="Kepada Pemungut Selain Bendaharawan";
$arr_masukan["status"][2][4]="DPP Nilai Lain";
$arr_masukan["status"][2][5]="Deemed Pajak Masukan";
$arr_masukan["status"][2][6]="Penyerahan Lainnya";
$arr_masukan["status"][2][7]="Penyerahan yang PPN-nya Tidak Dipungut";
$arr_masukan["status"][2][8]="Penyerahan yang PPN-nya Dibebaskan";
$arr_masukan["status"][2][9]="Penyerahan Aktiva (Pasal 16D UU PPN)";
$arr_masukan["status"][3][""]="";
$arr_masukan["status"][3][1]="Kepada Pihak yang Bukan Pemungut PPN";
$arr_masukan["status"][3][2]="Kepada Pemungut Bendaharawan";
$arr_masukan["status"][3][3]="Kepada Pemungut Selain Bendaharawan";
$arr_masukan["status"][3][4]="DPP Nilai Lain";
$arr_masukan["status"][3][5]="Deemed Pajak Masukan";
$arr_masukan["status"][3][6]="Penyerahan Lainnya";
$arr_masukan["status"][3][7]="Penyerahan yang PPN-nya Tidak Dipungut";
$arr_masukan["status"][3][8]="Penyerahan yang PPN-nya Dibebaskan";
$arr_masukan["status"][3][9]="Penyerahan Aktiva (Pasal 16D UU PPN)";

$arr_masukan_["status"][""][""]="";
$arr_masukan_["status"][1][""]="";
$arr_masukan_["status"][1][1]="BKP";
$arr_masukan_["status"][1][2]="BKP";
$arr_masukan_["status"][2][""]="";
$arr_masukan_["status"][2][1]="Kepada Pihak yang Bukan Pemungut PPN";
$arr_masukan_["status"][3][""]="";
$arr_masukan_["status"][3][1]="Kepada Pihak yang Bukan Pemungut PPN";
$arr_masukan_["status"][3][2]="Kepada Pemungut Bendaharawan";
$arr_masukan_["status"][3][3]="Kepada Pemungut Selain Bendaharawan";
$arr_masukan_["status"][3][4]="DPP Nilai Lain";
$arr_masukan_["status"][3][5]="Deemed Pajak Masukan";
$arr_masukan_["status"][3][6]="Penyerahan Lainnya";
$arr_masukan_["status"][3][7]="Penyerahan yang PPN-nya Tidak Dipungut";
$arr_masukan_["status"][3][8]="Penyerahan yang PPN-nya Dibebaskan";
$arr_masukan_["status"][3][9]="Penyerahan Aktiva (Pasal 16D UU PPN)";

$arr_masukan["dokumen"][""][""]="";
$arr_masukan["dokumen"][1][""]="";
$arr_masukan["dokumen"][1][1]="PIB dan SSP";
$arr_masukan["dokumen"][1][2]="SSP";
$arr_masukan["dokumen"][2][""]="";
$arr_masukan["dokumen"][2][1]="Faktur Pajak Standar";
$arr_masukan["dokumen"][2][2]="Nota Retur";
$arr_masukan["dokumen"][2][3]="Dokumen Yang Diperlakukan sebagai FP Standar";
$arr_masukan["dokumen"][2][4]="Faktur Batal";
$arr_masukan["dokumen"][2][5]="Faktur Pajak Pengganti";
$arr_masukan["dokumen"][3][""]="";
$arr_masukan["dokumen"][3][1]="Faktur Pajak Standar";
$arr_masukan["dokumen"][3][2]="PIB dan SSP";
$arr_masukan["dokumen"][3][3]="SSP";
$arr_masukan["dokumen"][3][4]="Nota Retur";
$arr_masukan["dokumen"][3][5]="Dokumen Yang Diperlakukan sebagai FP Standar";
$arr_masukan["dokumen"][3][6]="Faktur Batal";
$arr_masukan["dokumen"][3][7]="Faktur Pajak Pengganti";

$arr_masukan["diganti"][""][""]="";
$arr_masukan["diganti"][1][""]="";
$arr_masukan["diganti"][2][""]="";
$arr_masukan["diganti"][2][1]="Faktur Pajak Standar";
$arr_masukan["diganti"][2][2]="Dokumen Yang Diperlakukan sebagai FP Standar";
$arr_masukan["diganti"][3][""]="";

$arr_penghasilan_pph23=array("","8"=>"Jasa Profesi","9"=>"Jasa Konsultan Kecuali Konsultan Konstruksi","10"=>"Jasa Akuntansi dan Pembukuan","11"=>"Jasa Penilaian","12"=>"Jasa Aktuaris","13"=>"Jasa Teknik dan Jasa Manajemen","14"=>"Jasa Perancang Interior dan Jasa Perancang Pertamanan","15"=>"Jasa Perancang Mesin dan Jasa Perancang Peralatan","16"=>"Jasa Perancang Alat-alatTransportasi/Kendaraan","17"=>"Jasa Perancang Iklan/Logo","18"=>"Jasa Perancang Alat Kemasan","19"=>"Jasa Instalasi/ Pemasangan Mesin dan Jasa Instalasi/Pemasangan Peralatan","20"=>"Jasa Instalasi/Pemasangan Listrik/Telepon/Air/Gas/TV Kabel","21"=>"Jasa Perawatan/Pemeliharaan/Perbaikan Mesin dan Peralatan","22"=>"Jasa Perawatan/Pemeliharaan/Perbaikan Alat-alat Transportasi","23"=>"Jasa Perawatan/Pemeliharaan/Perbaikan Bangunan","24"=>"Jasa Pengeboran(Driling) dibidang Penambangan Migas","25"=>"Jasa Penambangan dan Jasa Penunjang di Bidang Penambangan Selain Migas","26"=>"Jasa Penunjang di Bidang Penerbangan dan Bandar Udara","27"=>"Jasa Penebangan Hutan Termasuk Land Clearing","28"=>"Jasa Pengolahan dan Pembuangan Limbah","29"=>"Jasa Maklon","30"=>"Jasa Rekruitmen/Penyediaan Tenaga Kerja","31"=>"Jasa Perantara","32"=>"Jasa di Bidang Perdagangan Surat-surat Berharga","33"=>"Jasa Kustodion/Penyimpanan/Penitipan, kecuali yang Dilakukan KSEI","34"=>"Jasa Telekomunikasi yang Bukan Untuk Umum","35"=>"Jasa Pengisian Sulih Suara (Dubbing) dan AtauMixing Film","36"=>"Jasa Pemanfaatan Informasi dibidang Teknologi Termasuk Jasa Internet","37"=>"Jasa Sehubungan dengan Software komputer","38"=>"Jasa Pelaksanaan Konstruksi","39"=>"Jasa Perencanaan Konstruksi","40"=>"Jasa Pengawasan Konstruksi","41"=>"Jasa Pembasmian Hama dan Jasa Pembersian","42"=>"Jasa Catering","43"=>"Jasa Selain Tersebut diatas yang Pembayaran di Bebankan pada APBN/APBD");

$var_link="cari=$cari&awalan=$awalan&src_kata=$src_kata&src_divisi=$src_divisi&src_jenis=$src_jenis&src_tahun=$src_tahun&src_bulan=$src_bulan&src_status_rkap=$src_status_rkap";

$level[1]="Not Access";
$level[2]="Read";
$level[3]="Read/Edit";
$level[4]="Read/Write";
$level[5]="All Access";

//$level[1]="Not Access";
//$level[2]="Read Only";
//$level[3]="Operator";
//$level[4]="Administrator";

function definisi_halaman($sql){
	global $inp_par, $Id;
#echo $hal;

#	echo "<pre>";
#	print_r($inp_par);
#	echo "</pre>";

	if($inp_par[awalan]==""){
		$inp_par[awalan] = 0;
		$next = $inp_par[awalan] + 1;
	}else{
		$awal_prev = $inp_par[awalan] - 1;
		$next=$inp_par[awalan] + 1;
	}
	if($inp_par[jumlah_per_hal]=="" || $inp_par[jumlah_per_hal]==0){
		$inp_par[jumlah_per_hal]=20;
	}
	$awalan_sql = $inp_par[awalan] * $inp_par[jumlah_per_hal];
	$res=dbresult($sql);
	$jumlah=pg_num_rows($res);
	$jml=ceil($jumlah/$inp_par[jumlah_per_hal]);
	$jmlakhir=$jml-1;
	$str_list.="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"text0\"><tr valign=top>";
	if($jumlah > $inp_par[jumlah_per_hal]){
		if($inp_par[awalan]==0){
			$str_list.="<td nowrap=\"nowrap\">".combo_jumlah_halaman($jml,var_kirim($inp_par,"awalan"))."&nbsp;</td><td nowrap=\"nowrap\"><a href=\"$link?Id=$Id&inp_par[awalan]=$next&inp_par[kata]=$inp_par[kata]&inp_par[divisi_kode]=$inp_par[divisi_kode]&inp_par[status]=$inp_par[status]".var_kirim($inp_par,"awalan")."\" title=\"Next Page\"><img src=../img/tombol-next.gif border=0></a><a href=\"$link?Id=$Id&inp_par[awalan]=$jmlakhir".var_kirim($inp_par,"awalan")."\" title=\"Last Page\"><img src=../img/tombol-last.gif border=0></a></td>";
		}else{
			$str_list.="<td nowrap=\"nowrap\"><a href=\"$link?Id=$Id&inp_par[awalan]=0".var_kirim($inp_par,"awalan")."\" title=\"First Page\"><img src=../img/tombol-first.gif border=0></a><a href=\"$link?Id=$Id&inp_par[awalan]=$awal_prev".var_kirim($inp_par,"awalan")."\" title=\"Prev Page\"><img src=../img/tombol-prev.gif border=0></a></td>";
			$str_list.="<td nowrap=\"nowrap\">&nbsp;".combo_jumlah_halaman($jml,var_kirim($inp_par,"awalan"))."&nbsp;</td>";
			if($inp_par[awalan]<($jml-1)){
				$str_list.="<td><a href=\"$link?Id=$Id&inp_par[awalan]=$next".var_kirim($inp_par,"awalan")."\" title=\"Next Page\"><img src=../img/tombol-next.gif border=0></a><a href=\"$link?Id=$Id&inp_par[awalan]=$jmlakhir".var_kirim($inp_par,"awalan")."\" title=\"Last Page\"><img src=../img/tombol-last.gif border=0></a></td>";
			}else{
				//	$str_list.="<font face=arial size=2 color=#527ACC>&nbsp;</font>";
			}
		}
	}
	$str_list.="</tr></table>";
	$res=dbresult($sql." limit ".$inp_par[jumlah_per_hal]." offset ".$awalan_sql);
	return array("tabel_halaman"=>$str_list,"res_sql"=>$res,"jml"=>$jml);
}
function definisi_halaman_biasa($sql){
	global $inp_par;
#echo $hal;
	if($inp_par[awalan]==""){
		$inp_par[awalan] = 0;
		$next = $inp_par[awalan] + 1;
	}else{
		$awal_prev = $inp_par[awalan] - 1;
		$next=$inp_par[awalan] + 1;
	}
	$awalan_sql = $inp_par[awalan] * $inp_par[jumlah_per_hal];
	$res=dbresult($sql);
	$jumlah=pg_num_rows($res);
	$jml=ceil($jumlah/$inp_par[jumlah_per_hal]);
	$jmlakhir=$jml-1;
	if($jumlah > $inp_par[jumlah_per_hal]){
		$str_list.="<div align=center><strong>Halaman : </strong>".combo_jumlah_halaman($jml,var_kirim($inp_par,"awalan"))."</div>";
	}
	$res=dbresult($sql." limit ".$inp_par[jumlah_per_hal]." offset ".$awalan_sql);
	return array("tabel_halaman"=>$str_list,"res_sql"=>$res,"jml"=>$jml);
}
function baca_permisi($sub_f_id){
	global $permisi;
	global $cuserid;
	global $st_level_user;
	$sql="select * from tbl_user a inner join tbl_menu_peran b on a.level_akses=cast(b.id_peran as bigint) where a.user_id=$cuserid and b.sub_f_id=$sub_f_id";
//        echo $sql;
	$res=dbresult($sql);//echo "$sql<br>";
	$jml=pg_num_rows($res);
	if($jml>0){
		$r=pg_fetch_array($res);
		switch($r[menu_access]){
			case 5:
					$permisi["add"]="izin";
					$permisi["edit"]="izin";
					$permisi["delete"]="izin";
					$permisi["access"]="izin";
					$permisi["copy"]="izin";
					$permisi["save"]="izin";
					$permisi["print"]="izin";
					$permisi["approve"]="izin";
					break;
					//AKSES ALL
			case 4:
					$permisi["add"]="izin";
					$permisi["edit"]="izin";
					$permisi["delete"]="tolak";
					$permisi["access"]="izin";
					$permisi["copy"]="tolak";
					$permisi["save"]="izin";
					$permisi["print"]="izin";
					$permisi["approve"]="tolak";
					break;
					//AKSES READ/WRITE
			case 3:
					$permisi["add"]="tolak";
					$permisi["edit"]="izin";
					$permisi["delete"]="tolak";
					$permisi["access"]="izin";
					$permisi["copy"]="tolak";
					$permisi["save"]="tolak";
					$permisi["print"]="tolak";
					$permisi["approve"]="tolak";
					break;
					//AKSES READ/EDIT
			case 2:
					$permisi["add"]="tolak";
					$permisi["edit"]="tolak";
					$permisi["delete"]="tolak";
					$permisi["access"]="izin";
					$permisi["copy"]="tolak";
					$permisi["save"]="tolak";
					$permisi["print"]="izin";
					$permisi["approve"]="tolak";
					break;
					//AKSES READ
			case 1:
					$permisi["add"]="tolak";
					$permisi["edit"]="tolak";
					$permisi["delete"]="tolak";
					$permisi["access"]="tolak";
					$permisi["copy"]="tolak";
					$permisi["save"]="tolak";
					$permisi["print"]="tolak";
					$permisi["approve"]="tolak";
					break;
					//AKSES NOT ACCESS
			case 0:
					$permisi["add"]="tolak";
					$permisi["edit"]="tolak";
					$permisi["delete"]="tolak";
					$permisi["access"]="tolak";
					$permisi["copy"]="tolak";
					$permisi["save"]="tolak";
					$permisi["print"]="tolak";
					$permisi["approve"]="tolak";
					break;
					//KOSONG
		}
	}
}

function e_getTotalDay($tahun,$bulan,$hari){
    if ($bulan==2){
        if ($tahun%4==0 && $hari>29){
            $hari = 29;
        }else if($tahun%4!=0 && $hari>28){
            $hari = 28;
        }
    }else if(($bulan==4 || $bulan==6 || $bulan==9 || $bulan==11) && $hari>30){
        $hari = 30;
    }
    return $hari;
}

function e_getIpAddr(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){ // check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function e_getDateNow(){
    return date("Y-m-d");
}

function e_getTimeNow(){
    return date("H:i:s");
}

function e_getDMYOnly($no,$control){
    if (strtolower($control)=='tahun'){
        $x='Y';
    }
    if(strtolower($control)=='bulan'){
        $x='n';
    }
    if (strtolower($control)=='tanggal'){
        $x='j';
    }
    $tanggal = date("$x");
    //$tanggalhsl = $no.$tanggal;
    return $tanggal;
}



function alertMsg($message, $form)
{
	echo "
		<script>
		alert('$message');
		window.location=('$form');
		</script>
		";
}
function location($form)
{
	echo "
		<script>
		window.location=('$form');
		</script>
		";
}
function getRSq($query) {
        global $conn;
	global $db;
        if(!isset($conn)){
		while(list($key,$isi)=each($db)){
			$opt .= "$key=$isi ";
		}
		$conn = pg_connect($opt);
	}

        $sql = pg_exec($conn, $query) or die(pg_errormessage($conn)."| ERROR : ".$query);
        //$rs = mysql_fetch_array($sql);
        //if($rs != "") {
        //    $temp = $rs;
        //}
        return $sql;
    }
//function getRSq($query) {
//        global $conn;
//	global $db;
//        if(!isset($conn)){
//                $conn = odbc_connect("Driver={PostgreSQL ODBC Driver(UNICODE)};Server=localhost;Database=$db[dbname]","$db[user]","$db[password]");
//		//$conn = odbc_connect("$db[dbname]","$db[user]","$db[password]");
//	}
//
//        $sql = odbc_exec($conn, $query) or die(odbc_error()."| ERROR : ".$query);
//        //$rs = mysql_fetch_array($sql);
//        //if($rs != "") {
//        //    $temp = $rs;
//        //}
//        return $sql;
//    }
function getRS($query) {
        $temp = "";
        global $conn;
	global $db;
        if(!isset($conn)){
		while(list($key,$isi)=each($db)){
			$opt .= "$key=$isi ";
		}
		$conn = pg_connect($opt);
	}
        $sql = pg_exec($conn, $query) or die(pg_errormessage($conn)."| ERROR : ".$query);
        $rs = pg_fetch_array($sql);
        if($rs != "") {
            $temp = $rs;
        }
        return $temp;
    }
//    function getRS($query) {
//        $temp = "";
//        global $conn;
//	global $db;
//        if(!isset($conn)){
//                $conn = odbc_connect("Driver={PostgreSQL ODBC Driver(UNICODE)};Server=localhost;Database=$db[dbname]","$db[user]","$db[password]");
//		//$conn = odbc_connect("$db[dbname]","$db[user]","$db[password]");
//	}
//        $sql = odbc_exec($conn, $query) or die(odbc_error()."| ERROR : ".$query);
//        $rs = odbc_fetch_array($sql);
//        if($rs != "") {
//            $temp = $rs;
//        }
//        return $temp;
//    }

    function kdauto($tabel,$inisial){
	$struktur = getRSq("SELECT DISTINCT * from $tabel");
	$field = pg_field_name($struktur,0);
	//$panjang = pg_field_len($struktur,1);
        //$panjang = pg_field_size($struktur,0);
        $panjang = 6;

	$qry = getRSq("SELECT max(".$field.") as id from ".$tabel);
        //echo "SELECT max(".$field.") as id from ".$tabel;
	$row = pg_fetch_array ($qry);

	if ($row['id']==""){
		$angka = 0;
		}else{
			$angka = substr($row['id'],strlen($inisial));
                        $angka = substr($angka,strlen("-"));
			}

	$angka++;
	$angka = strval($angka);
	$tmp = "";
	for ($i=1;$i<=($panjang-strlen($inisial)-strlen($angka)-1);$i++){
		$tmp = $tmp."0";
		}
		return $inisial."-".$tmp.$angka;
}

function getNameFromID($tabel,$fieldName,$fieldCond=""){
    $temp = "";
    if ($tabel!="" && $fieldName!=""){
    $rs = getRS("SELECT ".$fieldName." FROM ".$tabel." WHERE $fieldName!='' ".$fieldCond."");
    if ($rs!=""){
        $temp = $rs[$fieldName];
    }
    }
    return $temp;
}

function getQSearch($SQL,$SQLcari,$txtSearch,$Order,$Offset,$Limit,$tbSearch){
$sqlq = getRSq($SQLcari);
$jmlField = pg_num_fields($sqlq);
for ($x=1;$x<=$jmlField;$x++){
    $namaField = pg_field_name($sqlq, $x);
    $cond .= "lower(CAST(".$namaField." as TEXT)) LIKE lower('%$txtSearch%') OR ";
}
$condition = substr($cond,0,strlen($cond)-3);
// end field search
if(isset($tbSearch)){
	 $SQL .= " AND ($condition)";
         $SQL .= " ORDER BY $Order ASC";
         $SQL .= " OFFSET $Offset LIMIT $Limit ";
 }else{
         $SQL .= " ORDER BY $Order ASC";
         $SQL .= " OFFSET $Offset LIMIT $Limit ";
 }

 //$SQLtot = $SQL.$SQL1;
 return $SQL;
}

function getCari($sql,$sqlcari,$tambahan_sql=""){
    global $cari;
    global $src_kata;
    global $src_jenis;

    echo bar_cari($sql,"");
		if($cari<>""){
			if ($src_kata <> ""){
				$result=dbresult($sqlcari);
				$fnum= pg_num_fields($result);
				if ($src_jenis == -1){
					for ($i=0; $i<$fnum; $i++){
						$tambahan .= "lower(".pg_field_name($result, $i).") like lower('%$src_kata%') or ";
					}
					$tambahan = substr($tambahan,0,strlen($tambahan) - 3);
				}else{
					$j = $src_jenis;
					$tambahan = "lower(".pg_field_name($result, $j) .") like lower(('%$src_kata%'))";
				}
				if(strpos("$sql","where")>0){
					$sql = $sql ." and (". $tambahan .") $tambahan_sql";
				}else{
					$sql = $sql ." where ". $tambahan. $tambahan_sql;
				}
			}else{
				if(trim($tambahan_sql)<>""){
					if(strpos("$sql","where")>0){
						$sql = $sql ." and (true $tambahan_sql)";
					}else{
						$sql = $sql ." where true ". $tambahan_sql;
					}
				}
			}
		}
                //$temp .= tabel_view($sql,0,'','','#1A75BF','white','white',"100%","t",$sqlcari,$kriteria_tambahan,$template,$fld_jumlah_1,$fld_jumlah_2,$caption_1,$caption_2);
                return $sql;
}
//function baca_permisi($sub_f_id){
//	global $permisi, $cusername, $clevel, $st_level_user, $str_halaman;
//	$sql="select * from tbl_autority where user_name='$cusername' and sub_f_id=$sub_f_id";
//        //echo $sql;
//	$res=dbresult($sql);
//	$jml=pg_num_rows($res);
//	if($jml>0){
//		$r=pg_fetch_array($res,0);
//		$st_level_user=$r[level_id];
//		switch($st_level_user){
//			//Not Access
//			case 5:
//				$permisi["add"]="tolak";
//				$permisi["edit"]="tolak";
//				$permisi["delete"]="tolak";
//				$permisi["access"]="tolak";
//				$permisi["approve"]="tolak";
//				$permisi["print"]="tolak";
//				$permisi["privileges"]="tolak";
//				$permisi["logs"]="tolak";
//				$permisi["lock"]="tolak";
//				$permisi["disabled"]="tolak";
//				break;
//				//Read Only
//			case 4:
//				$permisi["add"]="tolak";
//				$permisi["edit"]="tolak";
//				$permisi["delete"]="tolak";
//				$permisi["access"]="izin";
//				$permisi["approve"]="tolak";
//				$permisi["print"]="izin";
//				$permisi["privileges"]="tolak";
//				$permisi["logs"]="tolak";
//				$permisi["lock"]="tolak";
//				$permisi["disabled"]="tolak";
//				break;
//
//				//Write Only
//			case 3:
//				$permisi["add"]="izin";
//				$permisi["edit"]="tolak";
//				$permisi["delete"]="tolak";
//				$permisi["access"]="izin";
//				$permisi["approve"]="tolak";
//				$permisi["print"]="izin";
//				$permisi["privileges"]="tolak";
//				$permisi["logs"]="tolak";
//				$permisi["lock"]="tolak";
//				$permisi["disabled"]="tolak";
//				break;
//
//				//Read Write
//			case 2:
//				$permisi["add"]="izin";
//				$permisi["edit"]="izin";
//				$permisi["delete"]="tolak";
//				$permisi["access"]="izin";
//				$permisi["approve"]="tolak";
//				$permisi["print"]="izin";
//				$permisi["privileges"]="tolak";
//				$permisi["logs"]="tolak";
//				$permisi["lock"]="tolak";
//				$permisi["disabled"]="tolak";
//				break;
//				//Administrator
//			case 1:
//				$permisi["add"]="izin";
//				$permisi["edit"]="izin";
//				$permisi["delete"]="izin";
//				$permisi["access"]="izin";
//				$permisi["approve"]="izin";
//				$permisi["print"]="izin";
//				$permisi["privileges"]="izin";
//				$permisi["logs"]="izin";
//				$permisi["lock"]="izin";
//				$permisi["disabled"]="tolak";
//				break;
//		}
//	}
//	/*$permisi["add"]="izin";
//	  $permisi["edit"]="izin";
//	  $permisi["delete"]="izin";
//	  $permisi["copy"]="izin";
//	  $permisi["view"]="izin";
//	  $permisi["logs"]="izin";
//	  $permisi["approve"]="izin";
//	  $permisi["access"]="izin";
//	  $permisi["print"]="izin";*/
//}

function user_access($sub_f_id){
	global $permisi, $cusername, $clevel, $st_level_user, $str_halaman;

	$sql="select * from tbl_autority where user_name='$cusername' and sub_f_id=$sub_f_id";
	$res=dbresult($sql);
	$jml=pg_num_rows($res);
	if($jml>0){
		$r=pg_fetch_array($res,0);
		$st_level_user=$r[level_id];
		switch($st_level_user){
			//Not Access
			case 5:
				$permisi["add"]="tolak";
				$permisi["edit"]="tolak";
				$permisi["delete"]="tolak";
				$permisi["access"]="tolak";
				$permisi["approve"]="tolak";
				$permisi["print"]="tolak";
				$permisi["privileges"]="tolak";
				$permisi["logs"]="tolak";
				$permisi["lock"]="tolak";
				$permisi["disabled"]="tolak";
				$permisi["view"]="tolak";
				break;

				//Read Only
			case 4:
				$permisi["add"]="tolak";
				$permisi["edit"]="tolak";
				$permisi["delete"]="tolak";
				$permisi["access"]="izin";
				$permisi["approve"]="tolak";
				$permisi["print"]="izin";
				$permisi["privileges"]="tolak";
				$permisi["logs"]="tolak";
				$permisi["lock"]="tolak";
				$permisi["disabled"]="tolak";
				$permisi["view"]="izin";
				break;

				//Write
			case 3:
				$permisi["add"]="izin";
				$permisi["edit"]="tolak";
				$permisi["delete"]="tolak";
				$permisi["access"]="izin";
				$permisi["approve"]="tolak";
				$permisi["print"]="izin";
				$permisi["privileges"]="tolak";
				$permisi["logs"]="tolak";
				$permisi["lock"]="tolak";
				$permisi["disabled"]="tolak";
				$permisi["view"]="izin";
				break;
				//ReadWrite
			case 2:
				$permisi["add"]="izin";
				$permisi["edit"]="izin";
				$permisi["delete"]="tolak";
				$permisi["access"]="izin";
				$permisi["approve"]="izin";
				$permisi["print"]="izin";
				$permisi["privileges"]="izin";
				$permisi["logs"]="izin";
				$permisi["lock"]="izin";
				$permisi["disabled"]="tolak";
				$permisi["view"]="izin";
				break;
				//Admin

			case 1:
				$permisi["add"]="izin";
				$permisi["edit"]="izin";
				$permisi["delete"]="izin";
				$permisi["access"]="izin";
				$permisi["approve"]="izin";
				$permisi["print"]="izin";
				$permisi["privileges"]="izin";
				$permisi["logs"]="izin";
				$permisi["lock"]="izin";
				$permisi["disabled"]="tolak";
				$permisi["view"]="izin";
				break;

			default:
				$permisi["add"]="tolak";
				$permisi["edit"]="tolak";
				$permisi["delete"]="tolak";
				$permisi["access"]="tolak";
				$permisi["approve"]="tolak";
				$permisi["print"]="tolak";
				$permisi["privileges"]="tolak";
				$permisi["logs"]="tolak";
				$permisi["lock"]="tolak";
				$permisi["disabled"]="tolak";
				$permisi["view"]="tolak";

				break;
		}
	}else{
		$permisi["add"]="tolak";
		$permisi["edit"]="tolak";
		$permisi["delete"]="tolak";
		$permisi["access"]="tolak";
		$permisi["approve"]="tolak";
		$permisi["print"]="tolak";
		$permisi["privileges"]="tolak";
		$permisi["logs"]="tolak";
		$permisi["lock"]="tolak";
		$permisi["disabled"]="tolak";
		$permisi["view"]="tolak";

	}
}

function tolakAkses(){

}

function minus($angka){
	if($angka < 0){
		$angka=$angka * -1;
		$out="<font color=\"red\">(".format_mede($angka).")</font>";
	}else{
		$out=format_mede($angka);
	}
	return $out;
}

$bulan[1]="Jan";
$bulan[2]="Feb";
$bulan[3]="Mar";
$bulan[4]="Apr";
$bulan[5]="May";
$bulan[6]="Jun";
$bulan[7]="Jul";
$bulan[8]="Aug";
$bulan[9]="Sep";
$bulan[10]="Oct";
$bulan[11]="Nov";
$bulan[12]="Des";

$nmBln[1]="Jan";
$nmBln[2]="Feb";
$nmBln[3]="Mar";
$nmBln[4]="Apr";
$nmBln[5]="May";
$nmBln[6]="Jun";
$nmBln[7]="Jul";
$nmBln[8]="Aug";
$nmBln[9]="Sep";
$nmBln[10]="Oct";
$nmBln[11]="Nov";
$nmBln[12]="Des";
$nmBlnPjg[1]="Januari";
$nmBlnPjg[2]="Februari";
$nmBlnPjg[3]="Maret";
$nmBlnPjg[4]="April";
$nmBlnPjg[5]="May";
$nmBlnPjg[6]="Juni";
$nmBlnPjg[7]="Juli";
$nmBlnPjg[8]="Agustus";
$nmBlnPjg[9]="September";
$nmBlnPjg[10]="Oktober";
$nmBlnPjg[11]="November";
$nmBlnPjg[12]="Desember";
$hp_berhasil=0;
$hp_gagal=0;

$arr_divisi2=array("WIL. I-SUMATERA","WIL. II - DKI JAKARTA & BODETABEK","WIL. III JABAR","WIL.IV - JATENG, D.I.YOGYAKARTA & KALIMANTAN","WIL. V - BALI, JATIM, NTB & NTT","WIL. VI - SULAWESI, MALUKU & PAPUA","DIVISI EPC");

$arr_divisi["x"]="All";
$arr_divisi["I"]="Wilayah I";
$arr_divisi["II"]="Wilayah II";
$arr_divisi["III"]="Wilayah III";
$arr_divisi["IV"]="Wilayah IV";
$arr_divisi["V"]="Wilayah V";
$arr_divisi["VI"]="Wilayah VI";
$arr_divisi["EPC"]="EPC";
$arr_divisi["KP"]="Kantor Pusat";
#$arr_divisi["F"]="---DBG";
#$arr_divisi["P"]="---EPC";

$arr_rumus_divisi["x"]=" and (divisi_kode like 'I%' or divisi_kode like 'II%' or divisi_kode like 'III%' or divisi_kode like 'IV%' or divisi_kode like 'V%' or divisi_kode like 'VI%' or divisi_kode like 'EPC%' or divisi_kode like 'KP%')";
$arr_rumus_divisi["I"]=" and (divisi_kode like 'I.%' or divisi_kode='I')";
$arr_rumus_divisi["II"]=" and (divisi_kode='II' or divisi_kode like 'II.%')";
$arr_rumus_divisi["III"]=" and (divisi_kode='III' or divisi_kode like 'III.%')";
$arr_rumus_divisi["IV"]=" and (divisi_kode='IV' or divisi_kode like 'IV.%')";
$arr_rumus_divisi["V"]=" and (divisi_kode='V' or divisi_kode like 'V.%')";
$arr_rumus_divisi["VI"]=" and (divisi_kode='VI' or divisi_kode like 'VI.%')";
$arr_rumus_divisi["EPC"]=" and (divisi_kode='EPC' or divisi_kode like 'EPC%')";
$arr_rumus_divisi["KP"]=" and (divisi_kode='KP' or divisi_kode like 'KP%')";

$sql_log=",user_update as \"USER\",tgl_update as \"TANGGAL\",ip_update as \"IP\",divisi_update as \"UNIT USAHA\",waktu_update as \"WAKTU\"";
$sql_log_cari=",user_update,tgl_update,ip_update,divisi_update,waktu_update";
$ip_update=$REMOTE_ADDR;
$tgl_update=date('Y-m-d');//echo $tgl_update;
$waktu_update=date('H:i:s');//echo $waktu_update;
function datediff($per,$d1,$d2) {
	$d = $d2-$d1;
	switch($per) {
		case "yyyy": $d/=12;
		case "m": $d*=12*7/365.25;
		case "ww": $d/=7;
		case "d": $d/=24;
		case "h": $d/=60;
		case "n": $d/=60;
	}
	return round($d);
}

function dateadd($per,$n,$d) {
	switch($per) {
		case "yyyy": $n*=12;
		case "m":
			$d=mktime(date("H",$d),date("i",$d)
					,date("s",$d),date("n",$d)+$n
					,date("j",$d),date("Y",$d));
		$n=0; break;
		case "ww": $n*=7;
		case "d": $n*=24;
		case "h": $n*=60;
		case "n": $n*=60;
	}
	return $d+$n;
}

#start check password
function check_pass(){
	global $cusername;
	if($cusername == ""){
		header("Location: index.php");
	}
}
#format tgl dari db 2007/01/01 -> 1 Jan 2007
function date_view($tanggal,$tipe=""){
	global $bulan;
	list($thn, $bln, $tgl) = split('[/.-]',$tanggal);
#$thn=substr($thn,-2);
#$tmp_tanggal = $tgl." ".$bulan["$bln"]." ".$thn;
	if($tipe==""){
		$tmp_tanggal = $tanggal==""?"":$tgl."/".$bln."/".$thn;
	}else{
		$tmp_tanggal = $tgl." ".nama_bulan($bln)." ".$thn;
	}

	return $tmp_tanggal;
}
function date_viewminggu($tanggal){
	global $bulan;
	list($thn, $bln, $tgl) = split('[/.-]',$tanggal);
#$thn=substr($thn,-2);
	($tgl<15)?$tgl="M(1+2)":$tgl="M(3+4)";
#$tmp_tanggal = $tgl." ".$bulan["$bln"]." ".$thn;
	$tmp_tanggal = $tgl." - ".$bln." - ".$thn;
	return $tmp_tanggal;
}
function pageLoad($awal,$akhir){
	$ret = datediff("s",$awal,$akhir)." Realisasi detik";
	return $ret;
}

function bar_cari($nm_table,$aksi,$muncul_divisi=""){
	global $src_kata;
	global $src_jenis;
	global $src_divisi;
	global $src_tahun;
	global $src_bulan;
	global $stmuncul;
	global $cari;
	global $arr_divisi;
	$bgheader  = "darkblue";
	$result=dbresult($nm_table);
	$fnum= pg_numfields($result);
	$num = pg_numrows($result); 
	$txt = "<form action=$aksi?stmuncul=$stmuncul method=post><table border=0 align=center class=text0>";
	$txt .= "<td align=right width=90%> <b>
		Search : <input type=text size=10 name=src_kata value='$src_kata' class=form2> In : <select name=src_jenis class=form2><option value=-1>All</option>";
	for ($i=1; $i < $fnum - 1; $i++){
		if($i == $src_jenis){
			$txt .= "<option value=$i selected>".pg_fieldname($result, $i)."</option>";
		}else{
			$txt .= "<option value=$i>".pg_fieldname($result, $i)."</option>";
		}
	}
	$txt .= "</select>";


	if($muncul_divisi <>""){
		$txt .= "
			Wilayah : ".combo_array_new("src_divisi",$arr_divisi,$src_divisi)." Bulan :".combo_array_new("src_bulan",array("All","1"=>"Jan","2"=>"Feb","3"=>"Mar","4"=>"Apr","5"=>"May","6"=>"Jun","7"=>"Jul","8"=>"Aug","9"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Des"),$src_bulan)." "." Tahun :".combo_array_new("src_tahun",array("2002"=>"2002","2003"=>"2003","2004"=>"2004","2005"=>"2005","2006"=>"2006","2007"=>"2007","2008"=>"2008","2009"=>"2009","2010"=>"2010"),$src_tahun)." ";
	}
	$txt .= "
		</b></td><td align=left width=30%><input type=submit value=\"Go &raquo\" name=cari class=tombolgo></td></tr></table></form>";
	return $txt;
}



function combo_bulan($nama,$sel,$opt_all="",$dis="f", $java="",$id=""){
	global $bulan;
	if($dis=="t"){
		$disable="disabled";
	}
	if($id!=""){
		$id=" id=\"$id\"";
	}
	//$bulan=array(1=>"Jan","Feb","Mar","Apr","Mei","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	$txtout="<select name=$nama class=form2 $disable $java $id>";
	if($opt_all!=""){
		$txtout .= "<option value='$opt_all' $tulis>".$opt_all."</option>";
	}
	$sel=intval($sel);
	for ($i=1;$i<=12;$i++){
		if ($i==$sel){
			$tulis =" selected=\"selected\"";
		}
		$txtout .= "<option value=$i $tulis>".$bulan[$i]."</option>\n";
		$tulis = "";
	}
	$txtout .= "</select>";
	return $txtout;
}

function combo_tanggal($nama,$sel,$awal,$akhir,$opt_all="",$dis="f", $java="", $id=""){
	if($dis=="t"){
		$disable="disabled";
	}
	if($id!=""){
		$id=" id=\"$id\"";
	}else{
		$id=$nama;
	}
	$txtout="<select name=\"$nama\" class=form2 $disable $java $id>";
	if($opt_all!=""){
		$txtout .= "<option value='$opt_all' $tulis>".$opt_all."</option>";
	}
	for ($i=$awal;$i<=$akhir;$i++){
		$i=strlen($i)==1?"0".$i:$i;
		if ($i==$sel){
			$tulis =" selected";
		}
		$txtout .= "<option value=$i $tulis>".$i."</option>\n";
		$tulis = "";
	}
	$txtout .= "</select>";
	return $txtout;
}


function cari_nilai($sql){
	$res=dbresult($sql);
	$r=pg_fetch_row($res);
	return $r[0];
}

function dbresult_on($sql){
	global $conn_on;
	global $db_on;
	if(!isset($conn_on)){
		while(list($key,$isi)=each($db_on)){
			$opt .= "$key=$isi ";
		}
		$conn_on = pg_connect($opt);
	}
	//echo $sql."<br>";
	if(!$result_on = pg_exec($conn_on, $sql)){
		echo "Proses ke database gagal";
		pg_errormessage($conn);	

		echo $sql."<br>";
	}
	return $result_on;
}

function param_link(){
	global $REQUEST_URI;
	global $mode;
	global $permisi;
	global $obj;
	global $kode;
	global $cari,$src_divisi,$src_tahun,$src_bulan,$src_status_rkap,$p_dini_mulai1,$p_dini_mulai2;
	global $cusername,$src_pemilik,$src_status_pq;
	global $awalan;
	global $order;
	global $src_kata;
	global $src_jenis,$divisi,$pemilik,$sumberdana,$lokasi;
	$link_cari="&awalan=$awalan&src_kata=$src_kata&src_bulan=$src_bulan&src_jenis=$src_jenis&src_divisi=$src_divisi&src_tahun=$src_tahun&cari=$cari&order=$order&src_status_rkap=$src_status_rkap&p_dini_mulai1=$p_dini_mulai1&p_dini_mulai2=$p_dini_mulai2&src_pemilik=$src_pemilik&src_status_pq=$src_status_pq";
	return $link_cari;
}

function format_kurung($nilai){
	$hasil=$nilai<0?"(".format_mede($nilai*-1).")":format_mede($nilai);
	$hasil=($hasil=="0.00" or $hasil=="(0.00)")?"&nbsp;":$hasil;
	return $hasil;
}

function format_mede($angka,$digit=2){
	return number_format($angka,$digit);
}

function tgl_mede($tgl){
	$arrtgl=split("-",$tgl);	
	return "$arrtgl[2]-$arrtgl[1]-$arrtgl[0]";
}

function persen_mede($pembilang,$penyebut){
	if(($penyebut==0)or($penyebut=="")){
		$hasil=0;
	}else{
		$hasil=$pembilang/$penyebut*100;
	}
	return format_mede($hasil);
}

function tambahnol($panjang,$nilai){
	$panjangnilai=strlen($nilai);
	for($i=1;$i<=($panjang-$panjangnilai);$i++){
		$tambah.="0";
	}
	$res=$tambah.$nilai;
	return $res;
}

function tabel_view($nm_table,$id,$link="/balifocus/tea_admin.php",$obj="",$bgjudul="ffffff",$warnatulisan="000000",$bgisi="ffffff",$width = "98%",$kondisi="t",$sqlcari="",$kreteria_tambahan="",$template="",$fld_jumlah_1="",$fld_jumlah_2="",$caption_1="",$caption_2=""){
	global $REQUEST_URI;
	global $mode,$nospk;
	global $permisi;
	global $obj;
	global $kode;
	global $cari,$src_divisi,$src_tahun;
	global $cusername;
	global $awalan;
	global $order,$sql_propinsi,$spl_prop;
	global $src_kata;
	global $src_jenis,$nospk;
	global $subbidang,$propinsi,$divisi_kode;
	$jumlah_per_hal = 20;
	//echo $nm_table;
	if($awalan==""){
		$awalan = 0;
		$next = $awalan + 1;
	}else{
		$awal_prev = $awalan - 1;
		$next=$awalan + 1;
	}
	$awalan_sql = $awalan * $jumlah_per_hal;
	$bgheader  = "darkblue";
	$result=dbresult($nm_table);
	$result=dbresult($nm_table);
	$result_cari=dbresult($sqlcari);
	$jumlah = pg_num_rows($result);
	if($order==""){
		$order = pg_field_name($result_cari,1);
	}
	$result=dbresult($nm_table ." order by ".$order." limit ".$jumlah_per_hal." offset ".$awalan_sql);
	//cari_subjumlah
	$fnum= pg_num_fields($result);
	$num = pg_num_rows($result); 
	//echo $nm_table;
	$jml=ceil($jumlah/$jumlah_per_hal);
	$jmlakhir=$jml-1;
	if($subbidang<>"" and $propinsi<>"" and $divisi_kode<>""){
		$propinsi=rawurlencode($propinsi);
		$tmbhlink="&subbidang=$subbidang";
		$tmbhlink1="&propinsi=$propinsi";
		$tmbhlink2="&divisi_kode=$divisi_kode";
	}
	if($nospk<>""){
		$tmbhspk="&nospk=$nospk";
	}
	$link_cari="&src_kata=$src_kata&src_jenis=$src_jenis&src_divisi=$src_divisi&src_tahun=$src_tahun&cari=$cari$tmbhlink$tmbhlink1$tmbhlink2";
	//$link_cari=str_replace(" ","spasi",$link_cari);
	//$link_cari=rawurlencode($link_cari);
	$txt .= "<table width=$width align=center border=0 cellpadding=3 cellspacing=1 bgcolor=\"C7DCC4\" class=tbl>\n
		<tr class=header>\n
		<td valign=top align=center width=30><font color=\"#FFFFFF\"><b>NO</b></font></td>\n";
	for ($i=0; $i<$fnum; $i++){
		if($kondisi=="t"){
			//if($i <> $id){
			if($i<$fnum-1){
				$str_order="&order=".pg_field_name($result_cari, $i);
			}
			$txt .= "<td align=center><a href=$link?$link_cari$str_order style='TEXT-DECORATION: none'><font color=\"#FFFFFF\"><b>".pg_field_name($result, $i)."</b></font></a></td>\n";
			//}
		}else{
			$txt .= "<td align=center><a href=$link?order=".pg_field_name($result_cari, $i)." style='TEXT-DECORATION: none'><font color=white><b>"
				.pg_field_name($result, $i)."</b></font></a></td>\n";
		}
	}
	$txt .= "</tr>\n";

	$jml = pg_num_rows($result);
	$jml_colom = pg_num_fields($result);
	$i=0;
	//echo $nm_table."<br>";
	//while(pg_fetch_row($result) ) {  
	for ($i=0; $i<$jml; $i++){
		pg_fetch_row($result,$i);
		if($warna_isi=="#F7F7F7"){
			$warna_isi="#ffffff";
		}else{
			$warna_isi="#F7F7F7";
		}
		$txt.= "<tr onmouseover=\"this.style.backgroundColor='#FFFF99';\" style=\"background-color: $warna_isi;\" onmouseout=\"this.style.backgroundColor='$warna_isi';\">\n";
		$txt .= "<td align=center valign=top>".($awalan_sql+$i+1)."</td>\n";
		for ($j=0; $j<$jml_colom; $j++){
			$txt .= "<td valign=top>".pg_result($result,$j)."</td>\n";
		}
		//$i=$i+1;
		$txt .="</tr>\n";
	}
	if($subbidang<>"" and $propinsi<>"" and $divisi_kode<>""){
		$tmbhlink="&subbidang=$subbidang";
		$tmbhlink1="&propinsi=$propinsi";
		$tmbhlink2="&divisi_kode=$divisi_kode";
	}
	if($nospk<>""){
		$tmbhspk="&nospk=$nospk";
	}
	if(isset($cusername)){
		$txt .= "<tr class=header><td colspan=".($jml_colom+1).">";
		if($permisi["add"]=="izin"){
			$txt .= "<input type=\"button\" name=\"Submit3\" value=\"Tambah Data\" class=\"tomboladd\" onclick='javascript:window.location=\"$link?mode=add&obj=$obj$tmbhspk$tmbhlink$tmbhlink1$tmbhlink2&kode=$kode$kreteria_tambahan\";'>";
		}
		$txt .= "</td></tr>\n";
	}
	$txt .= "</table>";
	if($order){
		$s_order="&order=$order";
	}else{
		$s_order="";
	}
	if($jumlah > $jumlah_per_hal){
		if($awalan==0){	
			$kata2="<font face=arial size=2 color=blue><a href=$link?awalan=$next$link_cari$s_order><img src=../img/tombol-next.gif alt=\"Halaman Berikutnya\" border=0></a><a href=$link?awalan=$jmlakhir$link_cari$s_order><img src=../img/tombol-last.gif alt=\"Halaman Akhir\" border=0></a></font>";
		}else{
			$kata1="<a href=$link?awalan=0$link_cari$s_order><img src=../img/tombol-first.gif alt=\"Halaman Akhir\" border=0></a><font face=arial size=2 color=blue><a href=$link?awalan=$awal_prev$link_cari$s_order><img src=../img/tombol-prev.gif alt=\"Halaman Sebelumnya\" border=0></a></font>";
			if($awalan<($jml-1)){
				$kata2="<font face=arial size=2 color=blue><a href=$link?awalan=$next$link_cari$s_order><img src=../img/tombol-next.gif alt=\"Halaman Berikutnya\" border=0></a><a href=$link?awalan=$jmlakhir$link_cari$s_order><img src=../img/tombol-last.gif alt=\"Halaman Akhir\"  border=0></a></font>";
			}
		}
	}
	$txt .= "<table align=center width=100%><tr><td align=center>$kata1 $kata2</td></tr></table>";
	return $txt;
}
$NamaBulan[1]="Januari";
$NamaBulan[2]="Februari";
$NamaBulan[3]="Maret";
$NamaBulan[4]="April";
$NamaBulan[5]="Mei";
$NamaBulan[6]="Juni";
$NamaBulan[7]="Juli";
$NamaBulan[8]="Agustus";
$NamaBulan[9]="September";
$NamaBulan[10]="Oktober";
$NamaBulan[11]="November";
$NamaBulan[12]="Desember";
function tabel_view_2($nm_table,$id,$link="/balifocus/tea_admin.php",$obj="",$bgjudul="ffffff",$warnatulisan="000000",$bgisi="ffffff",$width = "100%",$kondisi="t",$sqlcari="",$kreteria_tambahan="",$template="",$fld_jumlah_1="",$fld_jumlah_2="",$caption_1="",$caption_2=""){
	global $REQUEST_URI;
	global $mode;
	global $permisi;
	global $obj;
	global $kode;
	global $cari,$src_kata,$src_divisi,$src_jenis,$src_tahun,$src_bulan,$src_status_rkap,$p_dini_mulai1,$p_dini_mulai2;
	global $cusername,$src_pemilik,$src_status_pq;
	global $awalan;
	global $order;
	global $src_kata;
	global $src_jenis;
	$jumlah_per_hal = 20;
	//echo $nm_table;
	if($awalan==""){
		$awalan = 0;
		$next = $awalan + 1;
	}else{
		$awal_prev = $awalan - 1;
		$next=$awalan + 1;
	}
	$awalan_sql = $awalan * $jumlah_per_hal;
	$bgheader  = "darkblue";
	$result=dbresult($nm_table);
	$result=dbresult($nm_table);
	$result_cari=dbresult($sqlcari);
	$jumlah = pg_numrows($result);
	if($order==""){
		$order = pg_fieldname($result_cari,1);
	}
	$result=dbresult($nm_table ." order by ".$order." limit ".$jumlah_per_hal." offset ".$awalan_sql);
	//cari_subjumlah
	//echo $nm_table ." order by ".$order." limit ".$jumlah_per_hal." offset ".$awalan_sql;
	$posisi_from =strpos("$nm_table","from");
	$sql_1=substr($nm_table,$posisi_from,strlen($nm_table));
	if($fld_jumlah_1<>""){
		$str_fld_jumlah.=" sum($fld_jumlah_1)as jumlah_1";
	}
	if($fld_jumlah_2<>""){
		$str_fld_jumlah.=",sum($fld_jumlah_2)as jumlah_2";
	}

	/*
	   $sql_sub_jumlah = "select $str_fld_jumlah $sql_1  limit ".$jumlah_per_hal." offset ".$awalan_sql;
	   echo $sql_sub_jumlah;
	   $res_sub_jumlah=dbresult($sql_sub_jumlah);
	   if(pg_numrows($res_sub_jumlah)>0){
	   $r=pg_fetch_array($res_sub_jumlah,0);
	   $sub_jumlah_1=number_format($r["jumlah_1"]);
	   if(pg_numfields($res_sub_jumlah)){
	   $sub_jumlah_2=number_format($r["jumlah_2"]);
	   }
	   }
	 */
	if($fld_jumlah_1<>""){
		$sql_jumlah = "select $str_fld_jumlah $sql_1";
		$res_sub_jumlah=dbresult($sql_jumlah);
		//	echo $sql_jumlah;
		if(pg_numrows($res_sub_jumlah)>0){
			$r=pg_fetch_array($res_sub_jumlah,0);
			$jumlah_1=number_format($r["jumlah_1"],2);
			if(pg_numfields($res_sub_jumlah)){
				$jumlah_2=number_format($r["jumlah_2"],2);
			}
		}
	}
	//$res_sub_jumlah="";	
	//akhir cari_subjumlah

	$fnum= pg_numfields($result);
	$num = pg_numrows($result); 
	$jml=ceil($jumlah/$jumlah_per_hal);
	$jmlakhir=$jml-1;
	$link_cari="&src_kata=$src_kata&src_bulan=$src_bulan&src_jenis=$src_jenis&src_divisi=$src_divisi&src_tahun=$src_tahun&cari=$cari&order=$order&src_status_rkap=$src_status_rkap&p_dini_mulai1=$p_dini_mulai1&p_dini_mulai2=$p_dini_mulai2&src_pemilik=$src_pemilik&src_status_pq=$src_status_pq";
	if($jumlah > $jumlah_per_hal){
		if($awalan==0){	
			$kata2="<font face=arial size=2 color=blue><a href=$link?awalan=$next$link_cari><img src=gambar/tombol-next.gif alt=\"Halaman Berikutnya\" width=18 height=18 border=0></a> &nbsp;<a href=$link?awalan=$jmlakhir$link_cari><img src=gambar/tombol-last.gif alt=\"Halaman Akhir\" width=18 height=18 border=0></a></font>";
		}else{
			$kata1="<a href=$link?awalan=0$link_cari><img src=gambar/tombol-first.gif alt=\"Halaman Akhir\" width=18 height=18 border=0></a> &nbsp;<font face=arial size=2 color=blue><a href=$link?awalan=$awal_prev$link_cari><img src=gambar/tombol-prev.gif alt=\"Halaman Sebelumnya\" width=18 height=18 border=0></a></font>";
			if($awalan<($jml-1)){
				$kata2="<font face=arial size=2 color=blue><a href=$link?awalan=$next$link_cari><img src=gambar/tombol-next.gif alt=\"Halaman Berikutnya\" width=18 height=18 border=0></a> &nbsp;<a href=$link?awalan=$jmlakhir$link_cari><img src=gambar/tombol-last.gif alt=\"Halaman Akhir\" width=18 height=18 border=0></a></font>";
			}
		}
	}
	$txt = "<table width=$width border=0 align=center><tr><td align=center bgcolor=#eeeeee><table width=$width border=0 cellpadding=1 cellspacing=1 bgcolor=#d0ebf6><tr bgcolor=$bgheader>";
	$txt .= "<td bgcolor=$bgjudul valign=top align=center width=30><font color=\"#FFFFFF\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>NO</b></font></td>";
	for ($i=0; $i < $fnum; $i++){
		if($kondisi=="t"){
			if($i <> $id){
				if($i<$fnum-1){
#$str_order="&order=".pg_fieldname($result_cari, $i);
				}
				$txt .= "<td bgcolor=$bgjudul valign=top align=center><a href=$link?$link_cari$str_order style='TEXT-DECORATION: none'><font color=\"#FFFFFF\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>".pg_fieldname($result, $i)."</b></font></a></td>";
			}
		}else{
			$txt .= "<td bgcolor=$bgjudul valign=top align=center><a href=$link?order=".pg_fieldname($result_cari, $i)." style='TEXT-DECORATION: none'><font face=verdana size=2 color=white><b>"
				.pg_fieldname($result, $i)."</b></font></a></td>";
		}
	}
	/*
	   if(isset($cusername)){
	   $txt .= "<td bgcolor=$bgjudul align=center><font color=\"#FFFFFF\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b> ??</b></font></td>";
	   }
	 */
	$txt .= "</tr>";

	$txt .= "<tr bgcolor=#6699ff><td valign=top align=center><font color=\"#FFFFFF\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">1</font></td>";
	for ($i=0; $i < $fnum; $i++){
		if($kondisi=="t"){
			if($i <> $id){
				if($i<$fnum-1){
#	$str_order="&order=".pg_fieldname($result_cari, $i);
				}
				$txt .= "<td valign=top align=center><font color=\"#FFFFFF\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">".(1+$i)."</font></td>";
			}
		}else{
			$txt .= "<td bgcolor=$bgjudul valign=top align=center><font face=verdana size=2 color=white>".(1+$i)."</font></td>";
		}
	}
	$txt .= "</tr>";
	$jml = pg_numrows($result);
	$jml_colom = pg_numfields($result);
	for ($i=0; $i<$jml; $i++){
		$r_xx = pg_fetch_array($result,$i);
		if($caption_1<>""){
			$tmp_var=trim(str_replace(",","",$r_xx[$caption_1]));
			$tmp_var=trim(str_replace("<div align=right>","",$tmp_var));
			$tmp_var=trim(str_replace("</div>","",$tmp_var));
			$tmp_var=$tmp_var;
			$sub_jumlah_1+=$tmp_var;
		}
		if($caption_2<>""){
			$tmp_var=trim(str_replace(",","",$r_xx[$caption_2]));
			$tmp_var=trim(str_replace("<div align=right>","",$tmp_var));
			$tmp_var=trim(str_replace("</div>","",$tmp_var));
			$tmp_var=$tmp_var;
			$sub_jumlah_2+=$tmp_var;
		}

		if($warna_isi=="#E8EFFF"){
			$warna_isi="#ffffff";
		}else{
			$warna_isi="#E8EFFF";
		}
		$txt.= "<tr bgcolor=$warna_isi>";
		$r = pg_fetch_row($result,$i);
		$txt .= "<td valign=top align=center><font size=1 face=\"Verdana, Arial, Helvetica, sans-serif\">".($awalan_sql+$i+1)."</font></td>";
		for ($j=0; $j<count($r); $j++){
			if($kondisi=="t"){
				if($j <> $id){
					if($j==($id+1)){
						$txt .= "<td valign=top><a href=$link?kode=".trim($r[$id])."&mode=add".param_link()."><font size=1 face=\"Verdana, Arial, Helvetica, sans-serif\">$r[$j]</font></a></td>";
					}elseif($j==($id+count($r)-1)){
						$txt .= "<td valign=top width=75><font size=1 face=\"Verdana, Arial, Helvetica, sans-serif\">$r[$j]</font></td>";
					}else{
						$txt .= "<td valign=top><font size=1 face=\"Verdana, Arial, Helvetica, sans-serif\">$r[$j]</font></td>";
					}
				}
			}else{
				if($j==$id){
					$txt .= "<td valign=top><a href=$link?kode=".trim($r[$id])."&mode=add><font face=arial size=2 color=#006699><b>$r[$j]&nbsp;</b></font></a></td>";
				}else{
					$txt .= "<td valign=top><font face=verdana size=1><b>$r[$j]&nbsp;</b></font></td>";
				}
			}
		}
		/*
		   if(isset($cusername)){
		   $txt .= "<td valign=top>[<a href=$link?kode=".trim($r[$id])."&mode=edit$kreteria_tambahan><font face=arial size=2 color=#006699><b>Edit</b></font></a>][<a href=$link?kode=".trim($r[$id])."&mode=delete$kreteria_tambahan><font face=arial size=2 color=#006699><b>Delete</b></font></a>]</td>";
		   }
		 */
		$txt .="</tr>";
	}
	$sub_jumlah_1=number_format($sub_jumlah_1,2);
	$sub_jumlah_2=number_format($sub_jumlah_2,2);
	if($template<>""){
		eval("\$template = \"$template\";"); 
		$txt.="$template";
	}
#if(isset($cusername)){
#if($permisi["add"]=="izin"){
	$txt .= "<tr><td colspan=$jml_colom bgcolor=#ffffff><table width=100%><tr><td width=50%><a href=$link?mode=add&obj=$obj&kode=$kode$kreteria_tambahan><img src=gambar/tombol-add.gif border=0></a></td><td width=50% align=right>$kata1 $kata2</td></tr></table></td></tr>";
#}
#}
	$txt .= "</table></td></tr></table><div align=center></div>";
	return $txt;
}
function combo_ya_tidak($nama,$nilai){
	$txtout.="<select name=$nama>";	
	if($nilai=="t"){
		$txtout.="<option value=t selected>Ya</option>";
		$txtout.="<option value=f>Tidak</option>";
	}else{
		$txtout.="<option value=t>Ya</option>";
		$txtout.="<option value=f selected>Tidak</option>";
	}
	$txtout.="</select>";
	return $txtout;
}

function baca_ya_tidak($nilai){
	if($nilai=="t"){
		$txtout.="Ya";
	}else{
		$txtout.="Tidak";
	}
	return $txtout;
}

function terbilang($bilangan,$mu="RP") {
	$bilangan=number_format($bilangan,0,"","");

	$angka = array('0','0','0','0','0','0','0','0','0','0',
			'0','0','0','0','0','0');
	$kata = array('','Satu','Dua','Tiga','Empat','Lima',
			'Enam','Tujuh','Delapan','Sembilan');
	$tingkat = array('','Ribu','Juta','Milyar','Triliun');

	$panjang_bilangan = strlen($bilangan);

	/* pengujian panjang bilangan */
	if ($panjang_bilangan > 15) {
		$kalimat = "Diluar Batas";
		return $kalimat;
	}

	/* mengambil angka-angka yang ada dalam bilangan,
	   dimasukkan ke dalam array */
	for ($i = 1; $i <= $panjang_bilangan; $i++) {
		$angka[$i] = substr($bilangan,-($i),1);
	}

	$i = 1;
	$j = 0;
	$kalimat = "";


	/* mulai proses iterasi terhadap array angka */
	while ($i <= $panjang_bilangan) {

		$subkalimat = "";
		$kata1 = "";
		$kata2 = "";
		$kata3 = "";

		/* untuk ratusan */
		if ($angka[$i+2] != "0") {
			if ($angka[$i+2] == "1") {
				$kata1 = "Seratus";
			} else {
				$kata1 = $kata[$angka[$i+2]] . " Ratus";
			}
		}

		/* untuk puluhan atau belasan */
		if ($angka[$i+1] != "0") {
			if ($angka[$i+1] == "1") {
				if ($angka[$i] == "0") {
					$kata2 = "Sepuluh";
				} elseif ($angka[$i] == "1") {
					$kata2 = "Sebelas";
				} else {
					$kata2 = $kata[$angka[$i]] . " Belas";
				}
			} else {
				$kata2 = $kata[$angka[$i+1]] . " Puluh";
			}
		}

		/* untuk satuan */
		if ($angka[$i] != "0") {
			if ($angka[$i+1] != "1") {
				$kata3 = $kata[$angka[$i]];
			}
		}

		/* pengujian angka apakah tidak nol semua,
		   lalu ditambahkan tingkat */
		if (($angka[$i] != "0") OR ($angka[$i+1] != "0") OR
				($angka[$i+2] != "0")) {
			$subkalimat = "$kata1 $kata2 $kata3 " . $tingkat[$j] . " ";
		}

		/* gabungkan variabe sub kalimat (untuk satu blok 3 angka)
		   ke variabel kalimat */
		$kalimat = $subkalimat . $kalimat;
		$i = $i + 3;
		$j = $j + 1;

	}

	/* mengganti satu ribu jadi seribu jika diperlukan */
	if (($angka[5] == "0") AND ($angka[6] == "0")) {
		$kalimat = str_replace("Satu ribu","Seribu",$kalimat);
	}
	$mu_lain = cari_nilai("select nama_matauang from tbl_dm_mata_uang where kode_matauang = '$mu'");
	$kata_mu=$mu=="RP"?"Rupiah":$mu_lain;
	#return trim($kalimat."Rupiah");
	return trim($kalimat.$kata_mu);
}

function var_combo(){
	global $propinsi,$subbidang,$divisi_kode;
	global $c_propinsi,$c_subbidang,$cdivisi_kode;
	if($propinsi!=""){
		setcookie("c_propinsi",$propinsi);
	}else{
		$propinsi=$c_propinsi;
		setcookie("c_propinsi",$propinsi);
	}
	if($subbidang!=""){
		setcookie("c_subbidang",$subbidang);
	}else{
		$subbidang=$c_subbidang;
		setcookie("c_subbidang",$subbidang);
	}
	if($divisi_kode!=""){
		setcookie("c_divisi_kode",$divisi_kode);
	}else{
		$divisi_kode=$cdivisi_kode;
		setcookie("c_divisi_kode",$divisi_kode);
	}
	//echo $divisi_kode;

}

function namaKota($kode){
	$res=dbresult("select * from tbl_kota where kt_kode='$kode'");
	$n=pg_fetch_array($res);
	$nama=$n[kt_nama];
	return $nama;
}

function combo_kota($nmtable,$nilaifld,$muncul,$nama,$option="All",$nilai="",$cek_sql="",$java="",$sid="",$disabled="f"){
	if($disabled=="t"){$dis="disabled";}
	$txt = "<select name=\"$nama\" size=1 class=\"form2\" id=\"$sid\" $java $dis>";
	if($cek_sql == ""){
		$result=dbresult("select * from $nmtable order by $muncul");
		$a="select * from $nmtable order by $nilaifld";
	}else{
		$result=dbresult("$nmtable");
	}
	$jml = pg_numrows($result);
	if($option=="All"){
		$txt .="<option value=-1>$option</option>\n";
	}

	if($option=="0"){
		if($jml < 1){
			$txt .="<option value=''>$option</option>";	
		}
	}	

	if($option==" "){
		$txt .="<option value=''>$option</option>";
	}


	for ($i=0;$i<$jml;$i++){
		$r = pg_fetch_array($result,$i);
		if(trim($r[$nilaifld])==trim($nilai)){
			$txt .= "<option value='$r[$nilaifld]' selected>$r[$muncul]</option>\n";
		}else{
			$txt .= "<option value='$r[$nilaifld]'>$r[$muncul]</option>\n";
		}
#		echo $r[$nilaifld]."<nr>";
	}
	$txt .= "</select>";
	return $txt;
}
function combo212($nmtable,$nilaifld,$muncul,$nama,$option="All",$nilai="",$cek_sql="",$java="",$sid="",$disabled="f",$width=""){

	$width=$width==""?"":"style=\"width:$width\"";
	if($disabled=="t"){$dis="disabled";}
	$txt = "<select name=\"$nama\" size=1 class=\"form2\" id=\"$sid\" $java $dis $width>";
	if($cek_sql == ""){
		$result=dbresult("select * from $nmtable order by $nilaifld");
		$a="select * from $nmtable order by $nilaifld";
	}else{
		$result=dbresult("$nmtable");
	}
	$jml = pg_numrows($result);
	if($option=="All"){
		$txt .="<option value=-1>$option</option>\n";
	}

	if($option=="0"){
		if($jml < 1){
			$txt .="<option value=''>$option</option>";	
		}
	}	

	if($option==" "){
		$txt .="<option value=''>$option</option>";
	}

	for ($i=0;$i<$jml;$i++){
		$r = pg_fetch_array($result,$i);
		$title=$width==""?"":"title='$r[$muncul]'";
		if(trim($r[$nilaifld])==trim($nilai)){
			$txt .= "<option value='$r[$nilaifld]' $title selected>$r[$muncul]</option>\n";
		}else{
			$txt .= "<option value='$r[$nilaifld]' $title>$r[$muncul]</option>\n";
		}
	}
	$txt .= "</select>";
	return $txt;
}

function combo($nmtable,$nilaifld,$muncul,$nama,$option="All",$nilai="",$cek_sql="",$java="",$sid="",$disabled="f",$width=""){

	$width=$width==""?"":"style=\"width:$width\"";
	if($disabled=="t"){$dis="disabled";}
	$txt = "<select name=\"$nama\" size=1 class=\"form2\" id=\"$sid\" $java $dis $width>";
	if($cek_sql == ""){
		$result=dbresult("select * from $nmtable order by $nilaifld");
		$a="select * from $nmtable order by $nilaifld";
	}else{
		$result=dbresult("$nmtable");
	}
	$jml = pg_numrows($result);
	if($option=="All"){
		$txt .="<option value=-1>$option</option>\n";
	}

	if($option=="0"){
		if($jml < 1){
			$txt .="<option value=''>$option</option>";	
		}
	}	

	if($option==" "){
		$txt .="<option value=''>$option</option>";
	}

	for ($i=0;$i<$jml;$i++){
		$r = pg_fetch_array($result,$i);
		$title=$width==""?"":"title='$r[$muncul]'";
		if(trim($r[$nilaifld])==trim($nilai)){
			$txt .= "<option value='$r[$nilaifld]' $title selected>$r[$muncul]</option>\n";
		}else{
			$txt .= "<option value='$r[$nilaifld]' $title>$r[$muncul]</option>\n";
		}
//                echo $r[$nilaifld]." : ".$r[$muncul]."<br>";
	}
	$txt .= "</select>";
	return $txt;
}

function combo_awal($nmtable,$nilaifld,$muncul,$nama,$option="Pilih",$nilai="",$cek_sql="",$java="",$sid="",$disabled="f",$width=""){

	$width=$width==""?"":"style=\"width:$width\"";
	if($disabled=="t"){$dis="disabled";}
	$txt = "<select name=\"$nama\" size=1 class=\"text\" id=\"$sid\" $java $dis $width>";
	if($cek_sql == ""){
		$result=dbresult("select * from $nmtable order by $muncul");
		$a="select * from $nmtable order by $muncul";
	}else{
		$result=dbresult("$nmtable");
	}
	$jml = pg_numrows($result);
	if($option=="Pilih"){
		$txt .="<option value=-1>$option</option>\n";
	}

	if($option=="0"){
		if($jml < 1){
			$txt .="<option value=''>$option</option>";	
		}
	}	

	if($option==" "){
		$txt .="<option value=''>$option</option>";
	}

	for ($i=0;$i<$jml;$i++){
		$r = pg_fetch_array($result,$i);
		$title=$width==""?"":"title='$r[$muncul]'";
		if(trim($r[$nilaifld])==trim($nilai)){
			$txt .= "<option value='$r[$nilaifld]' $title selected>$r[$muncul]</option>\n";
		}else{
			$txt .= "<option value='$r[$nilaifld]' $title>$r[$muncul]</option>\n";
		}
	}
	$txt .= "</select>";
	return $txt;
}

function combo_two($nmtable,$nilaifld,$muncul,$muncul2,$nama,$option="All",$nilai="",$cek_sql="",$java="",$sid="",$disabled="f",$batas="."){
	if($disabled=="t"){$dis="disabled";}
	$txt = "<select name=\"$nama\" size=1 class=\"form2\" id=\"$sid\" $java $dis>";
	if($cek_sql == ""){
		$result=dbresult("select * from $nmtable order by $nilaifld");
		$a="select * from $nmtable order by $nilaifld";
	}else{
		$result=dbresult("$nmtable");
	}
	$jml = pg_numrows($result);
	if($option=="All"){
		$txt .="<option value=-1>$option</option>\n";
	}
	if($option==" "){
		$txt .="<option value=''>$option</option>";
	}
	for ($i=0;$i<$jml;$i++){
		$r = pg_fetch_array($result,$i);
		if(trim($r[$nilaifld])==trim($nilai)){
			$txt .= "<option value='$r[$nilaifld]' selected>$r[$muncul]$batas$r[$muncul2]</option>\n";
		}else{
			$txt .= "<option value='$r[$nilaifld]'>$r[$muncul]$batas$r[$muncul2]</option>\n";
		}
	}
	$txt .= "</select>";
	return $txt;
}

function arr_minggu(){
	$arr= array("Sun"=>0,"Mon"=>1,"Tue"=>2,"Wed"=>3,"Thu"=>4,"Fri"=>5,"Sat"=>6);
	return $arr;
}

function cari_M($kode_pro,$loka,$kode_cc,$kode_cm,$vol,$val){
	if($val=="Val"){
		if($vol==1){
			$volume=cari_nilai("select c_r0bvv from cost_c where c_pry='$kode_pro' and c_a='$loka' and c_kode='$kode_cc' and c_m='$kode_cm'");
		}else{
			$volume=cari_nilai("select c_r0bhv from cost_c where c_pry='$kode_pro' and c_a='$loka' and c_kode='$kode_cc' and c_m='$kode_cm'");
		}
	}else{
		if($vol==1){
			$volume=cari_nilai("select c_r0bvr from cost_c where c_pry='$kode_pro' and c_a='$loka' and c_kode='$kode_cc' and c_m='$kode_cm'");
		}else{
			$volume=cari_nilai("select c_r0bhr from cost_c where c_pry='$kode_pro' and c_a='$loka' and c_kode='$kode_cc' and c_m='$kode_cm'");
		}
	}
	return $volume;	
}

function combo_array($nama,$arr_nilai,$sel,$disabled="f",$java="",$option=""){
	if($disabled=="t"){$dis="disabled";}
	$txtout.="<select name=\"$nama\" id='$nama' class=\"form2\" $dis $java>\n";
	if($option==" "){
		$txtout.="<option value=''>$option</option>";
	}
	while(list($key,$nilai)=each($arr_nilai)){
		if(trim($nilai)===trim($sel)){
			$txtout.="<option value='$nilai' selected>$nilai</option>\n";
		}else{
			$txtout.="<option value='$nilai'>$nilai</option>\n";
		}

	}
	$txtout.="</select>";
	return $txtout;
}
function kepala2($text){
	$txt_out="
		<link href='link.css' type=text/css rel=stylesheet>
		<link href='style.css' type=text/css rel=stylesheet>
		<table width=\"98%\" height=\"35\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
		<tr>
		<td height=\"5\" colspan=\"2\"><img src=\"marketing/img/0.gif\" width=\"1\" height=\"10\"></td>
		</tr>
		<tr>
		<td width=\"25\" class=\"bgjdl\"><img src=\"logo-adhi.gif\" width=\"20\" height=\"20\"></td>
		<td height=\"25\" class=\"bgjdl\">$text</td>
		</tr>
		<tr>
		<td height=\"5\" colspan=\"2\"><img src=\"../img/0.gif\" width=\"1\" height=\"10\"></td>
		</tr>
		</table>";
	return $txt_out;
}

function combo_array2($nama,$arr_nilai,$sel,$disabled="f",$java="",$option="",$namaid=""){
	if($disabled=="t"){$dis="disabled";}
	if($namaid==""){$namaid=$nama;}
	$txtout.="<select name=\"$nama\" class=\"form2\" $dis $java id=\"$namaid\">\n";
	if($option==" "){
		$txtout .="<option value=''>$option</option>";
	}
	while(list($key,$nilai)=each($arr_nilai)){
		if(trim($key)===trim($sel)){
			$txtout.="<option value='$key' selected>$nilai</option>\n";	
		}else{
			$txtout.="<option value='$key'>$nilai</option>\n";
		}
	}
	$txtout.="</select>\n";
	return $txtout;
}


function divisi_combo($nama,$select,$java="",$sid="",$data=" "){
	global $cdivisi_kode;	
	$combo=combo("select divisi_kode,divisi_kode || ' -- ' || divisi_name as divisi_name from tbl_dm_divisi where divisi_kode not like '%00' order by divisi_kode","divisi_kode","divisi_name","$nama","$data","$select","t","$java","$sid");
	$text=cari_nilai("select divisi_kode || ' -- ' || divisi_name as divisi_name from tbl_dm_divisi where divisi_kode='$cdivisi_kode'")."<input type=\"hidden\" value=\"$cdivisi_kode\" name=\"$nama\" id=\"$sid\">";
	$hasil=preg_match("/00/i", $cdivisi_kode)?$combo:$text;
	return $hasil;
}

function combo_divisi(){
	global $divisi;
	$div_kode="where divisi_kode='I' or divisi_kode='II' or divisi_kode='III' or divisi_kode='IV' or divisi_kode='V' or divisi_kode='VI' or divisi_kode='EPC' or divisi_kode='KP'";
	$sqlp="select divisi_kode,divisi_name from tbl_divisi where position('.' in divisi_kode)=0 order by divisi_kode";
	$resp=dbresult($sqlp);//echo $sqlp."test<br>";
	while($p=pg_fetch_array($resp)){
		$sql_div_mbong=$p[divisi_kode].".";
		$sqlk="select  divisi_kode,divisi_name from tbl_divisi where divisi_kode like '$sql_div_mbong%' order by divisi_kode";
		$resk=dbresult($sqlk);//echo $sql1;
		$arr_div["$p[divisi_name]"]=$p[divisi_name];
		while($k=pg_fetch_array($resk)){
			$arr_div["$k[divisi_name]"]="-- $k[divisi_name]";
		}
	}

	$txt.="<td>".combo_array_new("divisi",$arr_div,$divisi)."</td>";
	return $txt;
}

function sqlkearray($sql){
	$res_item=dbresult($sql);
	while($r=pg_fetch_row($res_item)){
		$jumlah=count($r);
		if($jumlah==1){
			$arr_y["$r[0]"]=$r[0];
		}elseif($jumlah==2){
			$arr_y["$r[0]"]=$r[1];
		}elseif($jumlah==3){
			$arr_y["$r[0]"]["$r[1]"]=$r[2];
		}elseif($jumlah==4){
			$arr_y["$r[0]"]["$r[1]"]["$r[2]"]=$r[3];
		}
	}
	return $arr_y;
}

function data2array($sql,$nama_file,$nama_array="arr_x",$level=1){
	$res=dbresult($sql);
	if(!$tulisfile=fopen($nama_file,'w+')){
		print("Buka File : $nama_file Gagal");
		$hasil=false;
	}else{
		$isi_file="var $nama_array=new Array(); \n";
		while($r=pg_fetch_array($res)){

			for($i=0;$i<$level-1;$i++){
				$kunci=$r[pg_field_name($res,$i)];
				if($kunci!=$tmp_kunci[$i]){
					$tmp_kunci[$i]=$kunci;	
					for($j=0;$j<=$i;$j++){
						$str_kunci.="['".str_replace("'","",$tmp_kunci[$j])."']";
					}
					$isi_file.="$nama_array$str_kunci=new Array(); \n";
					$rs_kunci=$str_kunci;
					$str_kunci="";

				}
			}
			$nama=pg_field_name($res,$level-1);
			$nilai=pg_field_name($res,$level);


			$kunci=$rs_kunci."['".str_replace("'","",$r[$nama])."']";
			$isi=str_replace("'","",$r[$nilai]);
			$isi=str_replace(chr(13),"",$isi);
			$isi=str_replace("\^M","",$isi);
			$isi=str_replace("\n","",$isi);
			$isi=trim($isi);
			$isi_file.="$nama_array$kunci='$isi'; \n";
		}
	}
#echo $isi_file;
	if(!fwrite($tulisfile,$isi_file)){
		print("Penulisan File $nama_file Gagal");
		$hasil=false;
	}else{
		fclose($tulisfile);
	}
	return $hasil;
}

function data2arrayrekanan($sql,$nama_file,$nama_array="arr_rekanan",$level=1){
	$res=dbresult($sql);
	if(!$tulisfile=fopen($nama_file,'w+')){
		print("Buka File : $nama_file Gagal");
		$hasil=false;
	}else{
		$isi_file="var $nama_array=new Array(); \n";
		while($r=pg_fetch_array($res)){

			for($i=0;$i<$level-1;$i++){
				$kunci=$r[pg_field_name($res,$i)];
				if($kunci!=$tmp_kunci[$i]){
					$tmp_kunci[$i]=$kunci;	
					for($j=0;$j<=$i;$j++){
						$str_kunci.="['".str_replace("'","",$tmp_kunci[$j])."']";
					}
					$isi_file.="$nama_array$str_kunci=new Array(); \n";
					$rs_kunci=$str_kunci;
					$str_kunci="";
				}
			}
			$nama=pg_field_name($res,$level-1);
			$nilai=pg_field_name($res,$level);

			$kunci=$rs_kunci."['".str_replace("'","",$r[$nama])."']";
			$isi=str_replace("'","",$r[$nilai]);
			$isi=str_replace(chr(13),"",$isi);
			$isi=str_replace("^M","",$isi);
			$isi=str_replace("\n","",$isi);
			$isi=trim($isi);
			$isi_file.="$nama_array$kunci='$isi'; \n";
		}
	}
#echo $isi_file;
	if(!fwrite($tulisfile,$isi_file)){
		print("Penulisan File $nama_file Gagal");
		$hasil=false;
	}else{
		fclose($tulisfile);
	}
	return $hasil;
}

function data2arrayproyek($sql,$nama_file,$nama_array="arr_proyek",$level=1){
	$res=dbresult($sql);
	if(!$tulisfile=fopen($nama_file,'w+')){
		print("Buka File : $nama_file Gagal");
		$hasil=false;
	}else{
		$isi_file="var $nama_array=new Array(); \n";
		while($r=pg_fetch_array($res)){

			for($i=0;$i<$level-1;$i++){
				$kunci=$r[pg_field_name($res,$i)];
				if($kunci!=$tmp_kunci[$i]){
					$tmp_kunci[$i]=$kunci;	
					for($j=0;$j<=$i;$j++){
						$str_kunci.="['".str_replace("'","",$tmp_kunci[$j])."']";
					}
					$isi_file.="$nama_array$str_kunci=new Array(); \n";
					$rs_kunci=$str_kunci;
					$str_kunci="";
				}
			}
			$nama=pg_field_name($res,$level-1);
			$nilai=pg_field_name($res,$level);

			$kunci=$rs_kunci."['".str_replace("'","",$r[$nama])."']";
			$isi=str_replace("'","",$r[$nilai]);
			$isi=str_replace(chr(13),"",$isi);
			$isi=str_replace("\^M","",$isi);
			$isi=str_replace("\n","",$isi);
			$isi=trim($isi);
			$isi_file.="$nama_array$kunci='$isi'; \n";
		}
	}
#echo $isi_file;
	if(!fwrite($tulisfile,$isi_file)){
		print("Penulisan File $nama_file Gagal");
		$hasil=false;
	}else{
		fclose($tulisfile);
	}
	return $hasil;
}

function combo_java($nama,$nama_anak,$label,$arr,$arr_anak,$nilai,$even="f"){
	$load_nilai="loadAnak(document.all['$nama'],$arr,'$nilai');";
	if($even=="t"){
		$even="onchange=loadCombo(document.all['$nama_anak'],".$arr_anak."[document.all['$nama'][document.all['$nama'].selectedIndex].value],'');";
	}
	$txtout.="<SELECT size=1 name=$nama $even> <OPTION value=\"\" selected>$label</OPTION></SELECT>";
	$txtout.="<script>$load_nilai</script>";
	return $txtout;
}



function kondisi_divisi(){
	global $divisi;
	if($divisi=="Wilayah I" or $divisi=="Wilayah II" or $divisi=="Wilayah III" or $divisi=="Wilayah IV" or $divisi=="Wilayah V" or $divisi=="Wilayah VI" or $divisi=="Kantor Pusat" or $divisi=="EPC"){
		$mbong=$divisi;
	}else{
		$mbong=str_replace("--"," ",$divisi);
	}
	$sql2="select * from tbl_divisi where trim(divisi_name)='".trim($mbong)."'";
	$res2=dbresult($sql2);
	$r2=pg_fetch_array($res2);
	$txt=$r2[divisi_kode];
	return $txt;
}
function combo_pemilik(){
	global $pemilik;
	$sqlp="select pemilik_kode,pemilik_nama from tbl_pemilik_proyek where length(pemilik_kode)=2 order by pemilik_kode";
	$resp=dbresult($sqlp);//echo $sqlp."test<br>";
	while($p=pg_fetch_array($resp)){
		$sqlq="select pemilik_kode,pemilik_nama from tbl_pemilik_proyek where length(pemilik_kode)=5 and pemilik_kode like '$p[pemilik_kode]%' order by pemilik_kode";
		$resq=dbresult($sqlq);//echo $sql1;
		$arr_pemilik["$p[pemilik_nama]"]=$p[pemilik_nama];
		while($q=pg_fetch_array($resq)){
			$arr_pemilik["$q[pemilik_nama]"]="-- $q[pemilik_nama]";
			$sqlr="select pemilik_kode,pemilik_nama from tbl_pemilik_proyek where length(pemilik_kode)=8 and pemilik_kode like '$q[pemilik_kode]%' order by pemilik_kode";
			$resr=dbresult($sqlr);//echo $sql1;
			while($r=pg_fetch_array($resr)){
				$arr_pemilik["$r[pemilik_nama]"]="---- $r[pemilik_nama]";
			}	
		}
	}
	$txt.="<td>".combo_array_new("pemilik",$arr_pemilik,$pemilik)."</td>";
	return $txt;
}

function combo_sumberdana(){
	global $sumberdana;
	$sqlp="select sumberdana_kode,sumberdana_nama from tbl_sumber_dana where length(sumberdana_kode)=2 order by sumberdana_kode";
	$resp=dbresult($sqlp);//echo $sqlp."test<br>";
	while($p=pg_fetch_array($resp)){
		$sqlq="select sumberdana_kode,sumberdana_nama from tbl_sumber_dana where length(sumberdana_kode)=5 and sumberdana_kode like '$p[sumberdana_kode]%' order by sumberdana_kode";
		$resq=dbresult($sqlq);//echo $sql1;
		$arr_sumberdana["$p[sumberdana_nama]"]=$p[sumberdana_nama];
		while($q=pg_fetch_array($resq)){
			$arr_sumberdana["$q[sumberdana_nama]"]="-- $q[sumberdana_nama]";
			$sqlr="select sumberdana_kode,sumberdana_nama from tbl_sumber_dana where length(sumberdana_kode)=8 and sumberdana_kode like '$q[sumberdana_kode]%' order by sumberdana_kode";
			$resr=dbresult($sqlr);//echo $sql1;
			while($r=pg_fetch_array($resr)){
				$arr_sumberdana["$r[sumberdana_nama]"]="---- $r[sumberdana_nama]";
			}
		}
	}
	$txt.="<td>".combo_array_new("sumberdana",$arr_sumberdana,$sumberdana);
	return $txt;
}

function combo_array_new($nama,$arr_nilai,$sel,$aksi="",$opt_all="",$java=""){
	$txtout.="<select name=$nama $aksi class=form2 $java>";	
	if($opt_all<>""){
		$arr_all=split(",",$opt_all);
		$txtout.="<option value='$arr_all[1]'>$arr_all[0]</option>";	
	}
	while(list($key,$nilai)=each($arr_nilai)){
		if($key==$sel){
			$txtout.="<option value='$key' selected>$nilai</option>";	
		}else{
			$txtout.="<option value='$key'>$nilai</option>";	
		}
	}
	$txtout.="</select>";	
	return $txtout;
}


function fileupload($userfile,$userfile_name,$curdir_upload){
	if($userfile!=""){
		if(!is_dir("$curdir_upload/")){
			mkdir("$curdir_upload",0777);
			shell_exec("chmod -c 0777 $curdir_upload");
		}

		if(!copy($userfile,"$curdir_upload/$userfile_name")){ 
			return "error cuk"; 
		}else{
			return true;
		}
	}
} 

function namaBulan($bu){
	if($bu==1){
		$nama="Januari";
	}elseif($bu==2){
		$nama="Februari";
	}elseif($bu==3){
		$nama="Maret";
	}elseif($bu==4){
		$nama="April";
	}elseif($bu==5){
		$nama="Mei";
	}elseif($bu==6){
		$nama="Juni";
	}elseif($bu==7){
		$nama="Juli";
	}elseif($bu==8){
		$nama="Agustus";
	}elseif($bu==9){
		$nama="September";
	}elseif($bu==10){
		$nama="Oktober";
	}elseif($bu==11){
		$nama="November";
	}elseif($bu==12){
		$nama="Desember";
	}
	return $nama;
}

function list_voucher($sql,$sqlcari,$gambar='gambar/judul-user.gif',$status='f',$muncul_divisi="",$tambahan_sql="",$template="",$fld_jumlah_1="",$fld_jumlah_2="",$caption_1="",$caption_2=""){
	global $src_kata;
	global $src_jenis;
	global $cari;
	global $stprint;
	if ($status == ''){
		$status='f';	
	}
	if($stprint!="print"){
		//		echo kepala($gambar);
		echo bar_cari($sql,"",$muncul_divisi);	
	}
	if($cari<>""){
		if ($src_kata <> ""){
			$result=dbresult($sqlcari);
			$fnum= pg_numfields($result);
			if ($src_jenis == -1){
				for ($i=0; $i<$fnum; $i++){
					if(pg_field_type($result, $i)!="bool"){
						$tambahan .= "lower(".pg_fieldname($result, $i).") like lower('%$src_kata%') or ";
					}else{
						if((strtolower($src_kata)=="true") or (strtolower($src_kata)=="false")){
							$tambahan .= pg_fieldname($result, $i)." = '$src_kata' or ";
						}
					}
				}
				$tambahan = substr($tambahan,0,strlen($tambahan) - 3);
			}else{
				$j = $src_jenis;
				if(pg_field_type($result, $j)=="bool"){
					$tambahan = pg_fieldname($result, $j) ." = '$src_kata'";
				}else{
					$tambahan = "lower(".pg_fieldname($result, $j) .") like lower(('%$src_kata%'))";
				}
			}
			if(strpos("$sql","where")>0){
				$sql = $sql ." and (". $tambahan .") $tambahan_sql";
			}else{
				$sql = $sql ." where (". $tambahan.")". $tambahan_sql;
			}
		}else{
			if(trim($tambahan_sql)<>""){
				if(strpos("$sql","where")>0){
					$sql = $sql ." and (true $tambahan_sql)";
				}else{
					$sql = $sql ." where true ". $tambahan_sql;
				}
			}
		}
	}else{
		if(trim($tambahan_sql)<>""){
			if(strpos("$sql","where")>0){
				$sql = $sql ." and (true $tambahan_sql)";
			}else{
				$sql = $sql ." where true ". $tambahan_sql;
			}
		}
	}
	//echo $sql."<br>";
	$txtout.= tabel_view($sql,0,'','','#3366CC','white','white',"100%","t",$sqlcari,"",$template,$fld_jumlah_1,$fld_jumlah_2,$caption_1,$caption_2);
	return $txtout;
}

function tabel_array($sql){
	$res=dbresult($sql);
	$jml=pg_numrows($res);
	for($i=0;$i<$jml;$i++){
		$r=pg_fetch_row($res,$i);
		$arr_x["$r[0]"]=$r[1];
	}
	return $arr_x;
}

function tabel_array1($sql){
	$res=dbresult($sql);
	$jml=pg_num_rows($res);
	for($i=0;$i<=$jml-1;$i++){
		$r=pg_fetch_array($res,$i);
		$arr_x["$r[user_name]"]=$r[nama];
	}
	return $arr_x;
}

function hapus_div($var_str){
	$tmp_var=trim(str_replace(",","",$var_str));
	$tmp_var=trim(str_replace("<div align=right>","",$tmp_var));
	$tmp_var=trim(str_replace("</div>","",$tmp_var));
	$tmp_var=intval($tmp_var);
	return $tmp_var;
}

function generate_rule($nama_rule,$nm_obj,$pesan_rule="",$rg_awal="0",$rg_akhir="0"){
	global $str_aturan;
        //echo $nama_rule;
	switch($nama_rule){
		case "is_isi":$str_aturan.="t10_checkisi(document.all['$nm_obj'],\"$pesan_rule\");\n";break;
		case "is_email":$str_aturan.="t10_checkmail(document.all['$nm_obj'],\"$pesan_rule\");\n";break;
		case "is_date":$str_aturan.="t10_checkvaliddate(document.all['$nm_obj[0]'],document.all['$nm_obj[1]'],document.all['$nm_obj[2]']);\n";break;
		case "is_num":$str_aturan.="t10_checknum(document.all['$nm_obj'],\"$pesan_rule\");\n";
		break;
		case "is_range":$str_aturan.="t10_checkrange(document.all['$nm_obj'],\"$pesan_rule\",$rg_awal,$rg_akhir);\n";
		break;
                case "is_rangesd":$str_aturan.="t10_checkrangesd(document.all['$nm_obj'],\"$pesan_rule\",$rg_awal,$rg_akhir);\n";
		break;
                case "is_rangesdha":$str_aturan.="t10_checkrangesdha(document.all['$nm_obj'],\"$pesan_rule\",$rg_awal,$rg_akhir);\n";
		break;
	}

}
function generate_rules($nama_rule,$nm_obj,$pesan_rule="",$rg_awal="0",$rg_akhir="0"){
	global $str_aturan;
	switch($nama_rule){
		case "is_isi":$str_aturan.="t10_checkisi(document.getElementById('$nm_obj'),\"$pesan_rule\");\n";break;
		case "is_email":$str_aturan.="t10_checkmail(document.getElementById('$nm_obj'),\"$pesan_rule\");\n";break;
		case "is_date":$str_aturan.="t10_checkvaliddate(document.getElementById('$nm_obj[0]'),document.getElementById('$nm_obj[1]'),document.getElementById('$nm_obj[2]'));\n";break;
		case "is_num":$str_aturan.="t10_checknum(document.getElementById('$nm_obj'),\"$pesan_rule\");\n";
		break;
		case "is_range":$str_aturan.="t10_checkrange(document.getElementById('$nm_obj'),\"$pesan_rule\",$rg_awal,$rg_akhir);\n";
		break;
	}

}
function generate_validasi(){
	global $str_aturan;
	$txtout.='
		<SCRIPT language=javascript src=../fungsi_validasi.js></SCRIPT>
		<SCRIPT language=javascript>
		var valid;
	function validasi(var_nama_form) {
		var nm_form=var_nama_form;
		valid=true;
		//alert(document.all[\'inp[sss]\'].value);
		'.$str_aturan.';
			if (valid){
				return true
			}else{
				return false
			}
	}

	</SCRIPT>';
	return $txtout;
}

function nama_isi($tabel,$nmfield,$kondisi,$nama_awal,$nama_akhir){
	if(trim($nmfield)<>''){
		$arrnr=split(",",$nmfield);
		for($i=0;$i<count($arrnr);$i++){
			if(is_numeric($arrnr[$i])){
				$sql="select * from $tabel where $kondisi='$arrnr[$i]'";
				$res=dbresult($sql);
				if(pg_num_rows($res)>"0"){
					$p=pg_fetch_array($res);
					if($i=="0"){
						$nama=trim($p[$nama_awal])." ".trim($p[$nama_akhir]);
					}else{
						$nama=$nama.",".trim($p[$nama_awal])." ".trim($p[$nama_akhir]);
					}
				}
			}
		}
	}
	return $nama;
}

function combo_divisi_new($aksi="",$nama){
	global $divisi;
	$div_kode="where divisi_kode='I' or divisi_kode='II' or divisi_kode='III' or divisi_kode='IV' or divisi_kode='V' or divisi_kode='VI' or divisi_kode='EPC' or divisi_kode='KP'";
	$sqlp="select divisi_kode,divisi_name from tbl_divisi where position('.' in divisi_kode)=0 order by divisi_kode";
	$resp=dbresult($sqlp);//echo $sqlp."test<br>";
	while($p=pg_fetch_array($resp)){
		$sql_div_mbong=$p[divisi_kode].".";
		$sqlk="select  divisi_kode,divisi_name from tbl_divisi where divisi_kode like '$sql_div_mbong%' order by divisi_kode";
		$resk=dbresult($sqlk);//echo $sql1;
		$arr_div["$p[divisi_kode]"]=$p[divisi_name];
		while($k=pg_fetch_array($resk)){
			$arr_div["$k[divisi_kode]"]="-- $k[divisi_name]";
		}
	}
	$txt.="<td>".combo_array_new($nama,$arr_div,$divisi,$aksi)."</td>";
	return $txt;
}
function combo_jumlah_halaman($jml,$filter){
	global $inp_par, $Id;
	//global $jml;
	$txt.="<select name=inp_par[awalan] onChange=\"javascript:loncat('mainFrame',this,0)\" class=form2>";
	for ($i=1;$i<=$jml;$i++){
		if($inp_par[awalan]==($i-1)){
			$txt.="<option value=\"?Id=$Id&inp_par[awalan]=".($i-1)."$filter\" selected>Hal $i</option>";
		}else{
			$txt.="<option value=\"?Id=$Id&inp_par[awalan]=".($i-1)."$filter\">Hal $i</option>";
		}
	}
	$txt.="</select>";
	return $txt;
}

function kosong($angka){
	(is_numeric($angka)?$ret=$angka:$ret="0");

	return $ret;
}

function rumus_cf_proyek($arr,$tipe="y",$bln_sbl="",$rbp=""){
	global $inp_cook,$inp_par;

	//tipe != y untuk hitung rencana
	$sql="select nilai_kontrak, mulai from tbl_proyek where no_spk='$inp_cook[no_spk]'";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	$nilai_kontrak = $r[nilai_kontrak];
	$tanggal_mulai = $r[mulai];


	if($tipe!="y"){
		$param_tanggal = $tanggal_mulai;
	}else{
		if($bln_sbl!=""){
			$param_tanggal = $inp_par[cmb_tahun]."-".$bln_sbl."-1";
		}else{
			$param_tanggal = $inp_par[cmb_tahun]."-".$inp_par[cmb_bulan]."-1";
		}
	}

	/*
	   $sql="select nilai from tbl_bunga_pusat where tanggal=(select max(tanggal) from tbl_bunga_pusat where tanggal <= '$param_tanggal')";
	   $res=dbresult($sql);
	   $r=pg_fetch_array($res);
	   $nilai = $r[nilai];
	 */	
	$arr[2]=$nilai_kontrak*$arr[0]/100;					//--> Rencana Pendapatan
	$arr[3]=$nilai_kontrak*$arr[1]/100;					//--> Akumulasi Pendapatan

	$arr[13]=$arr[6]+$arr[7]+$arr[8]+$arr[9]+$arr[10]+$arr[11]+$arr[12];	//--> Jumlah Pemasukan Uang
	$arr[5]=$arr[13];							//--> Pemasukan Uang

	$arr[18]=$arr[19]+$arr[20];						//--> Sub Kontraktor
	$arr[21]=$arr[22]+$arr[23]+$arr[24]+$arr[25];				//--> Peralatan

	$arr[26]=$arr[16]+$arr[17]+$arr[18]+$arr[21];				//--> Sub Total A
	$arr[15]=$arr[26];
	//echo "before $arr[29] and rbp ";
	/*
	   if($rbp=="y"){
	   $arr[29]=$arr[29];
	   }else{
	   $arr[29]=($arr[44]*$nilai/100)/12;
	   }
	 */
	//echo "after $arr[29] <br>";
	$arr[27]=$arr[28]+$arr[29]+$arr[30]+$arr[31]+$arr[32]+$arr[33];		//--> Biaya Tidak Langsung
	$arr[34]=$arr[27];							//--> Sub Total B

	$arr[39]=$arr[36]+$arr[37]+$arr[38];					//--> Sub Total C
	$arr[35]=$arr[39];							//--> Kewajiban

	$arr[40]=$arr[15]+$arr[27]+$arr[35];					//--> Jumlah Pengeluaran Uang
	$arr[14]=$arr[40];		

	$arr[41]=$arr[4]+$arr[13]-$arr[40];					//--> Selisih (I+II-III)
	$arr[45]=$arr[44]+$arr[42]-$arr[43];					//--> Akumulasi Kredit Bank

	$arr[46]=$arr[42]+$arr[41]-$arr[43];					//--> Saldo Akhir Kas

	//echo "$param_tanggal $arr[45] $nilai / 100 <br />";
	return $arr;
}

function saldo_awal_ra_cf_proyek($inp_cook,$inp_par){
	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	$bulan=date("n",$tgl_sebelum);
	$tahun=date("Y",$tgl_sebelum);
	$sql="select * from tbl_cf_ra_proyek where no_spk='$inp_cook[no_spk]' and tahun='$tahun' and urutan=46";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	if(is_numeric($r["bulan_$bulan"])){
		return $r["bulan_$bulan"];
	}else{
		return 0;
	}
}

function saldo_awal_racf_divisi($inp_cook,$inp_par){
	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	$bulan=date("n",$tgl_sebelum);
	$tahun=date("Y",$tgl_sebelum);
	$sql="select * from tbl_cf_ra_divisi where divisi_kode='$inp_cook[divisi_kode]' and tahun='$tahun' and urutan='32'";
#echo "$sql<br>";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	if(is_numeric($r["bulan_$bulan"])){
		return $r["bulan_$bulan"];
	}else{
		return 0;
	}
}

function saldo_awal_ricf_divisi($inp_cook,$inp_par){
	$sql="select * from tbl_cf_ri_divisi where divisi_kode='$inp_cook[divisi_kode]' and tahun='$inp_par[cmb_tahun]' and urutan='32'";
	$minggu=$inp_par[cmb_minggu]-1;
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	if(is_numeric($r["minggu_$inp_par[cmb_bulan]_$minggu"])){
		return $r["minggu_$inp_par[cmb_bulan]_$minggu"];
	}else{
		return 0;
	}
}

function saldo_awal_procf_divisi($inp_cook,$inp_par){
	$sql="select * from tbl_cf_proyeksi_divisi where divisi_kode='$inp_cook[divisi_kode]' and tahun='$inp_par[cmb_tahun]' and rolling='$inp_par[cmb_tahun]-$inp_par[cmb_bulan_roll]-1' and urutan='32'";
	$minggu=$inp_par[cmb_minggu]-1;
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	if(is_numeric($r["minggu_$inp_par[cmb_bulan]_$minggu"])){
		return $r["minggu_$inp_par[cmb_bulan]_$minggu"];
	}else{
		return 0;
	}
}

function saldo_awal_ra_cf_pusat($inp_cook,$inp_par){
	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	$bulan=date("n",$tgl_sebelum);
	$tahun=date("Y",$tgl_sebelum);
	$sql="select * from tbl_cf_ra_pusat where divisi_kode='$inp_cook[pusat_kode]' and tahun='$tahun' and urutan=21";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	if(is_numeric($r["bulan_$bulan"])){
		return $r["bulan_$bulan"];
	}else{
		return 0;
	}
}
function saldo_awal_kredit_pusat($inp_cook,$inp_par){
	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	$bulan=date("n",$tgl_sebelum);
	$tahun=date("Y",$tgl_sebelum);
	$sql="select * from tbl_cf_ra_pusat where divisi_kode='$inp_cook[pusat_kode]' and tahun='$tahun' and urutan in (25,24)";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$nilai["$r[urutan]"]=is_numeric($r["bulan_$bulan"])?$r["bulan_$bulan"]:0;
	}
	$saldo=$nilai[25]+$nilai[24];
	return $saldo;
}
function saldo_awal_ra_cf_konsolidasi($inp_cook,$inp_par){
	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	$bulan=date("n",$tgl_sebelum);
	$tahun=date("Y",$tgl_sebelum);
	$sql="select * from tbl_cf_ra_konsolidasi where tahun='$tahun' and urutan=23";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	if(is_numeric($r["bulan_$bulan"])){
		return $r["bulan_$bulan"];
	}else{
		return 0;
	}
}
/*
   function rumus_cash_flow_pusat($arr){
   $arr[2]=$arr[3]+$arr[4]+$arr[5];
   $arr[7]=$r_piu[amount]==""?0:$r_piu[amount];
   $arr[8]=$r_pjk[amount]==""?0:$r_pjk[amount];
   $arr[6]=$arr[7]+$arr[8];
   $arr[10]=$arr[11]+$arr[12]+$arr[13]+$arr[14]+$arr[15];	
   $arr[1]=$arr[16]=$arr[2]+$arr[6]+$arr[9]+$arr[10];	

   $arr[18]=$arr[19]+$arr[20]+$arr[21]+$arr[22]+$arr[23]+$arr[24]+$arr[25]+$arr[26]+$arr[27]+$arr[28]+$arr[29]+$arr[30];
   $arr[31]=$arr[32]+$arr[33]+$arr[34]+$arr[35]+$arr[36]+$arr[37]+$arr[38];
   $arr[17]=$arr[41]=$arr[18]+$arr[31]+$arr[39]+$arr[40];

   $arr[42]=$arr[1]-$arr[17];
   $arr[43]=$arr[44]+$arr[45];

   $arr[47]=$arr[0]+$arr[42];

   return $arr;
   }
 */
function rumus_cash_flow_pusat($arr){
	$arr[0]=kosong($arr[0]);
	$arr[1]=kosong($arr[1]);
	$arr[2]=kosong($arr[2]);
	$arr[3]=kosong($arr[3]);
	$arr[4]=kosong($arr[4]);
	$arr[5]=kosong($arr[5]);
	$arr["5.1"]=kosong($arr["5.1"]);
	$arr["5.2"]=kosong($arr["5.2"]);
	$arr["5.3"]=kosong($arr["5.3"]);
	$arr["5.4"]=kosong($arr["5.4"]);
	$arr["5.5"]=kosong($arr["5.5"]);
	$arr[6]=kosong($arr[6]);
	$arr[7]=kosong($arr[7]);
	$arr[8]=kosong($arr[8]);
	$arr[9]=kosong($arr[9]);
	$arr[10]=kosong($arr[10]);
	$arr[11]=kosong($arr[11]);
	$arr[12]=kosong($arr[12]);
	$arr[13]=kosong($arr[13]);
	$arr[14]=kosong($arr[14]);
	$arr["14.1"]=kosong($arr["14.1"]);
	$arr["14.2"]=kosong($arr["14.2"]);
	$arr["14.3"]=kosong($arr["14.3"]);
	$arr["14.4"]=kosong($arr["14.4"]);
	$arr["14.5"]=kosong($arr["14.5"]);
	$arr[15]=kosong($arr[15]);
	$arr[16]=kosong($arr[16]);
	$arr[17]=kosong($arr[17]);
	$arr[18]=kosong($arr[18]);
	$arr[19]=kosong($arr[19]);
	$arr[20]=kosong($arr[20]);
	$arr[21]=kosong($arr[21]);
	$arr[22]=kosong($arr[22]);
	$arr["22.1"]=kosong($arr["22.1"]);
	$arr["22.2"]=kosong($arr["22.2"]);
	$arr[23]=kosong($arr[23]);
	$arr["23.1"]=kosong($arr["23.1"]);
	$arr["23.2"]=kosong($arr["23.2"]);
	$arr[24]=kosong($arr[24]);
	$arr[25]=kosong($arr[25]);
	$arr[26]=kosong($arr[26]);

	//Kredit
	$arr[22]=$arr["22.1"]+$arr["22.2"];						//penerimaan pengembalian kredit
	$arr[23]=$arr["23.1"]+$arr["23.2"];						//pengembalian kredit
	$arr[24]=$arr[22]-$arr[23];						//pemakain kredit
	$arr[26]=$arr[24]+$arr[25];						//Akumulasi kredit

	//Penerimaan
	$arr[5]=$arr["5.1"]+$arr["5.2"]+$arr["5.3"]+$arr["5.4"];                                //Pengembalian Provisi bg lc kmk
	$arr[2]=$arr[3]+$arr[4]+$arr[5];                                        //kewajiban wilayah
	$arr[6]=$arr[22];                                                      //penerimaan kredit
	$arr[7]=$arr[8]+$arr[9]+$arr[10];                                    //penerimaan pihak ke 3
	$arr[1]=$arr[2]+$arr[6]+$arr[7];                                      //penerimaan
	$arr[11]=$arr[1];                                                       //jumlah pemasukan

	//Pengeluaran
	$arr[14]=$arr["14.1"]+$arr["14.2"]+$arr["14.3"]+$arr["14.4"]+$arr["14.5"];                  //Wilayah lainnya
	$arr[15]=$arr[23];                                                      //pengembalian kredit
	$arr[12]=$arr[13]+$arr[14]+$arr[15]+$arr[16]+$arr[17]+$arr[18];//pengeluaran
#echo $arr[16];
	$arr[19]=$arr[12];                                                      //jumlah pengeluaran

	$arr[20]=$arr[1]-$arr[12];                                              //surplus
	$arr[21]=$arr[0]+$arr[20];                                              //saldo akhir kas bank
	return $arr;
}

function rumus_cash_flow_konsolidasi($arr){
	$arr[1]=kosong($arr[1]);
	$arr[2]=kosong($arr[2]);
	$arr[3]=kosong($arr[3]);
	$arr[4]=kosong($arr[4]);
	$arr[5]=kosong($arr[5]);
	$arr[6]=kosong($arr[6]);
	$arr[7]=kosong($arr[7]);
	$arr[8]=kosong($arr[8]);
	$arr[9]=kosong($arr[9]);
	$arr[10]=kosong($arr[10]);
	$arr[11]=kosong($arr[11]);
	$arr[12]=kosong($arr[12]);
	$arr[13]=kosong($arr[13]);
	$arr[14]=kosong($arr[14]);
	$arr[15]=kosong($arr[15]);
	$arr[16]=kosong($arr[16]);
	$arr[17]=kosong($arr[17]);
	$arr[18]=kosong($arr[18]);
	$arr[19]=kosong($arr[19]);
	$arr[20]=kosong($arr[20]);

	//mulai rumus
	$arr[3]=$arr[2];
	$arr[5]=$arr[4];
	$arr[6]=$arr[2]-$arr[4];
	$arr[7]=$arr[6];
	$arr[11]=$arr[12]+$arr[13];
	$arr[14]=$arr[15]+$arr[16];
	$arr[18]=$arr[9]+$arr[12]-$arr[15];
	$arr[19]=$arr[10]+$arr[13]-$arr[16];
	$arr[17]=$arr[18]+$arr[19];
	$arr[20]=$arr[1]+$arr[6]+$arr[11]-$arr[14];

	return $arr;
}
//saldoawalkonsolidasi
function saldo_awal_konsolidasi($bln,$thn){
	global $inp_par;
	if($bln == '1'){
		$bulans=12;
		$tahuns=$thn - 1;
	}else{
		$bulans=$bln - 1;
		$tahuns=$thn;
	}
	//kalo diambil dari konsolidasi doang
	$sql="select * from tbl_cf_ra_konsolidasi where urutan='20' and tahun='$tahuns'";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	$saldo_awal=$r["bulan_".$bulans];

	return $saldo_awal;
}

function rumus_ra_cf_konsolidasi($arr){
	global $inp_par;
	$bulan1 = "bulan_".$inp_par[cmb_bulan];


	//divisi
	$sql_wil="select ".$bulan1.", divisi_kode, urutan from tbl_cf_ri_divisi where divisi_kode not like '%.%' and urutan in(10,26,28,29) and tahun=$inp_par[cmb_tahun]";
	$res_wil=dbresult($sql_wil);
	while($r_wil=pg_fetch_array($res_wil)){
		if($r_wil[urutan]=="10"){
			$jns=2;
		}elseif($r_wil[urutan]=="26"){
			$jns=4;
		}elseif($r_wil[urutan]=="28"){
			$jns=13;
		}elseif($r_wil[urutan]=="29"){
			$jns=16;
		}
		$arr[$jns]["$r_wil[divisi_kode]"]=$r_wil["$bulan1"];
		//jml wilayah
		$jml_wb["$jns"]+=$r_wil["$bulan1"];

		//selisih
		$arr_wbo_wilayah["$r_wil[divisi_kode]"][6]=$arr_wbo_wilayah["$r_wil[divisi_kode]"][2]-$arr_wbo_wilayah["$r_wil[divisi_kode]"][4];
	}
	//load KP
	$sql="select * from tbl_cf_ri_pusat where urutan in(6,7,13,15,16,17,18) and tahun = '$inp_par[cmb_tahun]'";
	$res=dbresult($sql);
	$divisi_kode='KP';
	while($r=pg_fetch_array($res)){
		if($r[urutan]=='7'){
			$jenis="2";     //penerimaan
		}elseif($r[urutan]=='13' || $r[urutan]=='16' || $r[urutan]=='17' || $r[urutan]=='18'){
			$jenis="4";     //pengeluaran
		}elseif($r[urutan]=='6'){
			$jenis="13";    //penerimaan kredit
		}elseif($r[urutan]=='15'){
			$jenis="16";    //pengembalian kredit
		}
		if($jenis=='4'){
			$arr[$jenis]["KP"][$bulan1]+=$r[$bulan1];
		}else{
			$arr[$jenis]["KP"][$bulan1]=$r[$bulan1];
		}
		//jumlah perjenis total (wil + pusat)
		$jml_wb[$jenis]+=$r[$bulan1];
	}
	$jml_wb[2]=kosong($jml_wb[2]);
	$jml_wb[4]=kosong($jml_wb[4]);	
	$jml_wb[13]=kosong($jml_wb[13]);	
	$jml_wb[16]=kosong($jml_wb[16]);	

	if($inp_par[cmb_bulan]==1){
		$bulan2="bulan_12";
		$tahun2=($inp_par[cmb_tahun]-1);
	}else{
		$bulan2="bulan_".($inp_par[cmb_bulan]-1);
		$tahun2=$inp_par[cmb_tahun];
	}

	$sql="select * from tbl_cf_ra_konsolidasi where urutan in (1,17,18,19) and tahun='$tahun2'";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$s_kmk[$r[urutan]]=$r[$bulan2];
	}



	/*
	   $sql_wil="select ".$bulan1.", divisi_kode, urutan from tbl_cf_ri_divisi where divisi_kode not like '%.%' and urutan in(10,19,21,22) and tahun=$inp_par[cmb_tahun]";
	   $res_wil=dbresult($sql_wil);
	   while($r_wil=pg_fetch_array($res_wil)){
	   if($r_wil[urutan]=="10"){
	   $jns=2;
	   }elseif($r_wil[urutan]=="19"){
	   $jns=4;
	   }elseif($r_wil[urutan]=="21"){
	   $jns=13;
	   }elseif($r_wil[urutan]=="22"){
	   $jns=16;
	   }

	   $arr["$jns"]["$r_wil[divisi_kode]"]=$r_wil["$bulan1"];
	   $jml_wb["$jns"]+=$r_wil["$bulan1"];

	   $arr["6"]["$r_wil[divisi_kode]"]=$arr["2"]["$r_wil[divisi_kode]"]-$arr["4"]["$r_wil[divisi_kode]"];
	   }
	 */
#$arr[1]=saldo_awal_ra_cf_konsolidasi($inp_cook,$inp_par);
	$arr[1]=$s_kmk[1];
	$arr[2]=$jml_wb[2];	//penerimaan
	$arr[3]=$arr[2];	//jml penerimaan
	$arr[4]=$jml_wb[4];	//pengeluaran
	$arr[5]=$arr[4];	//jml pengeluaran
	$arr[6]=$arr[2]-$arr[4];//selisih
	$arr[7]=$arr[6];	//jml selisih

	$arr[8]=$s_kmk[17];		//saldo kmk
	$arr[9]=$s_kmk[18];		//sa kredit korporasi
	$arr[10]=$s_kmk[19];		//sa kmk

	$arr[13]=$jml_wb[13];	//penerimaan kmk
	$arr[16]=$jml_wb[16];	//pengembalian kmk


	$arr[11]=$arr[12]+$arr[13];	//penerimaan kredit
	$arr[14]=$arr[15]+$arr[16];	//pengembalian kredit

	$arr[18]=$arr[12]-$arr[15]+$arr[9];	//akumulasi korporasi
	$arr[19]=$arr[13]-$arr[16]+$arr[10];	//akumulasi kmk
	$arr[17]=$arr[18]+$arr[19];	//akumulasi kredit bank

	$arr[20]=$arr[1]+$arr[6]+$arr[11]-$arr[14];

	return $arr;
}
function rumus_ra_cf_pusat($arr,$inp_par,$inp_cook){
	$bulan = "bulan_".$inp_par[cmb_bulan];
	//dana talangan
	$sql_dat="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='4'";
	$res_dat=dbresult($sql_dat);
	$r_dat=pg_fetch_array($res_dat);
	//lain lain (kewajiban)
	$sql_wll="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='5.5'";
	$res_wll=dbresult($sql_wll);
	$r_wll=pg_fetch_array($res_wll);
	//piutang
	$sql_piu="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='8'";
	$res_piu=dbresult($sql_piu);
	$r_piu=pg_fetch_array($res_piu);
	//restitusi pajak
	$sql_pjk="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='9'";
	$res_pjk=dbresult($sql_pjk);
	$r_pjk=pg_fetch_array($res_pjk);
	//lain lain (penerimaan)
	$sql_tll="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='10'";
	$res_tll=dbresult($sql_tll);
	$r_tll=pg_fetch_array($res_tll);
	//kantor pusat
	$sql_kp="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='13'";
	$res_kp=dbresult($sql_kp);
	$r_kp=pg_fetch_array($res_kp);
	//wilayah lain lain
	$sql_kwl="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='14.5'";
	$res_kwl=dbresult($sql_kwl);
	$r_kwl=pg_fetch_array($res_kwl);
	//pembayaran kepada negara
	$sql_pkn="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='16'";
	$res_pkn=dbresult($sql_pkn);
	$r_pkn=pg_fetch_array($res_pkn);
	//pembayaran pihak ketiga
	$sql_pp3="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='17'";
	$res_pp3=dbresult($sql_pp3);
	$r_pp3=pg_fetch_array($res_pp3);
	//lain lain (pengeluaran)
	$sql_kll="select sum(".$bulan.") as amount from tbl_detail_rencana where tahun='$inp_par[cmb_tahun]' and urutan_induk='18'";
	$res_kll=dbresult($sql_kll);
	$r_kll=pg_fetch_array($res_kll);

	//setoran kewajiban
	$sql_wil="select sum(".$bulan.") as amount from tbl_cf_ra_divisi where divisi_kode not like '%.%' and divisi_kode!='$inp_cook[pusat_kode]' and urutan=17 and tahun=$inp_par[cmb_tahun]";
	$res_wil=dbresult($sql_wil);
	$r_wil=pg_fetch_array($res_wil);

	$arr[0]=saldo_awal_ra_cf_pusat($inp_cook,$inp_par);
	$arr[3]=$r_wil[amount]==""?0:$r_wil[amount];
	$arr[4]=$r_dat[amount]==""?0:$r_dat[amount];
	$arr["5.5"]=$r_wll[amount]==""?0:$r_wll[amount];
	$arr[8]=$r_piu[amount]==""?0:$r_piu[amount];
	$arr[9]=$r_pjk[amount]==""?0:$r_pjk[amount];
	$arr[10]=$r_tll[amount]==""?0:$r_tll[amount];
	$arr[13]=$r_kp[amount]==""?0:$r_kp[amount];
	$arr["14.5"]=$r_kwl[amount]==""?0:$r_kwl[amount];
	$arr[16]=$r_pkn[amount]==""?0:$r_pkn[amount];
	$arr[17]=$r_pp3[amount]==""?0:$r_pp3[amount];
	$arr[18]=$r_kll[amount]==""?0:$r_kll[amount];
	$arr[25]=saldo_awal_kredit_pusat($inp_cook,$inp_par);

	//Kredit
	$arr[22]=$arr["22.1"]+$arr["22.2"];						//penerimaan pengembalian kredit
	$arr[23]=$arr["23.1"]+$arr["23.2"];						//pengembalian kredit
	$arr[24]=$arr[22]-$arr[23];						//pemakain kredit
	$arr[26]=$arr[24]+$arr[25];						//Akumulasi kredit

	//Penerimaan
	$arr[5]=$arr["5.1"]+$arr["5.2"]+$arr["5.3"]+$arr["5.4"];                                //Pengembalian Provisi bg lc kmk
	$arr[2]=$arr[3]+$arr[4]+$arr[5];                                        //kewajiban wilayah
	$arr[6]=$arr[22];                                                      //penerimaan kredit
	$arr[7]=$arr[8]+$arr[9]+$arr[10];                                    //penerimaan pihak ke 3
	$arr[1]=$arr[2]+$arr[6]+$arr[7];                                      //penerimaan
	$arr[11]=$arr[1];                                                       //jumlah pemasukan

	//Pengeluaran
	$arr[14]=$arr["14.1"]+$arr["14.2"]+$arr["14.3"]+$arr["14.4"]+$arr["14.5"];                  //Wilayah lainnya
	$arr[15]=$arr[23];                                                      //pengembalian kredit
	$arr[12]=$arr[13]+$arr[14]+$arr[15]+$arr[16]+$arr[17]+$arr[18];//pengeluaran
#echo $arr[16];
	$arr[19]=$arr[12];                                                      //jumlah pengeluaran

	$arr[20]=$arr[1]-$arr[12];                                              //surplus
	$arr[21]=$arr[0]+$arr[20];                                              //saldo akhir kas bank

	return $arr;
}

function saldo_awal_ri_kredit_pusat($inp_cook,$inp_par){
	if($inp_par[cmb_minggu]==0 and $inp_par[cmb_bulan]==1){
		$minggu=4;
		$bulan=12;
		$tahun=$inp_par[cmb_tahun]-1;
	}else if($inp_par[cmb_minggu]==0 and $inp_par[cmb_bulan]!=1){
		$minggu=4;
		$bulan=$inp_par[cmb_bulan]-1;
		$tahun=$inp_par[cmb_tahun];
	}else{
		$minggu=$inp_par[cmb_minggu];
		$bulan=$inp_par[cmb_bulan];
		$tahun=$inp_par[cmb_tahun];
	}
	$sql="select * from tbl_cf_ri_pusat where no_spk='$inp_cook[pusat_kode]' and tahun='$tahun' and urutan in (24,25)";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$nilai["$r[urutan]"]=is_numeric($r["minggu_".$bulan."_".$minggu])?$r["minggu_".$bulan."_".$minggu]:0;
	}
	$saldo=$nilai[25]+$nilai[24];
	return $saldo;
}
function saldo_awal_ri_cf_pusat($inp_cook,$inp_par){
	if($inp_par[cmb_minggu]==0 and $inp_par[cmb_bulan]==1){
		$minggu=4;
		$bulan=12;
		$tahun=$inp_par[cmb_tahun]-1;
	}else if($inp_par[cmb_minggu]==0 and $inp_par[cmb_bulan]!=1){
		$minggu=4;
		$bulan=$inp_par[cmb_bulan]-1;
		$tahun=$inp_par[cmb_tahun];
	}else{
		$minggu=$inp_par[cmb_minggu];
		$bulan=$inp_par[cmb_bulan];
		$tahun=$inp_par[cmb_tahun];
	}
	$sql="select * from tbl_cf_ri_pusat where no_spk='$inp_cook[pusat_kode]' and tahun='$tahun' and urutan=26";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	if(is_numeric($r["minggu_".$bulan."_".$minggu])){
		return $r["minggu_".$bulan."_".$minggu];
	}else{
		return 0;
	}
}

function rumus_ri_cf_pusat($arr,$inp_par,$inp_cook){
	$minggu = "minggu_".$inp_par[cmb_bulan]."_".$inp_par[cmb_minggu];
	//dana talangan	
	$sql_dat="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='4'";
	$res_dat=dbresult($sql_dat);
	$r_dat=pg_fetch_array($res_dat);
	//lain lain (kewajiban)
	$sql_wll="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='5.5'";
	$res_wll=dbresult($sql_wll);
	$r_wll=pg_fetch_array($res_wll);
	//piutang
	$sql_piu="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='8'";
	$res_piu=dbresult($sql_piu);
	$r_piu=pg_fetch_array($res_piu);
	//restitusi pajak
	$sql_pjk="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='9'";
	$res_pjk=dbresult($sql_pjk);
	$r_pjk=pg_fetch_array($res_pjk);
	//lain lain penerimaan
	$sql_tll="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='10'";
	$res_tll=dbresult($sql_tll);
	$r_tll=pg_fetch_array($res_tll);
	//kantor pusat
	$sql_kp="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='13'";
	$res_kp=dbresult($sql_kp);
	$r_kp=pg_fetch_array($res_kp);
	//wilayah lain lain
	$sql_kwl="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='14.5'";
	$res_kwl=dbresult($sql_kwl);
	$r_kwl=pg_fetch_array($res_kwl);
	//pkn
	$sql_ppn="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='16'";
	$res_ppn=dbresult($sql_ppn);
	$r_pkn=pg_fetch_array($res_ppn);
	//pihak ketiga
	$sql_pp3="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='17'";
	$res_pp3=dbresult($sql_pp3);
	$r_pp3=pg_fetch_array($res_pp3);
	//lain lain pengeluaran
	$sql_kll="select sum(".$minggu.") as amount from tbl_detail_realisasi where tahun='$inp_par[cmb_tahun]' and urutan_induk='18'";
	$res_kll=dbresult($sql_kll);
	$r_kll=pg_fetch_array($res_kll);
	//setoran wilayah
	$sql_wil="select sum(".$minggu.") as amount from tbl_cf_ri_divisi where divisi_kode not like '%.%' and divisi_kode!='$inp_cook[pusat_kode]' and urutan=17 and tahun=$inp_par[cmb_tahun]";
	$res_wil=dbresult($sql_wil);
	$r_wil=pg_fetch_array($res_wil);

	$arr[0]=saldo_awal_ri_cf_pusat($inp_cook,$inp_par);
	$arr[3]=$r_wil[amount]==""?0:$r_wil[amount];
	$arr[4]=$r_dat[amount]==""?0:$r_dat[amount];
	$arr["5.5"]=$r_wll[amount]==""?0:$r_wll[amount];
	$arr[8]=$r_piu[amount]==""?0:$r_piu[amount];
	$arr[9]=$r_pjk[amount]==""?0:$r_pjk[amount];
	$arr[10]=$r_tll[amount]==""?0:$r_tll[amount];
	$arr[13]=$r_kp[amount]==""?0:$r_kp[amount];
	$arr["14.5"]=$r_kwl[amount]==""?0:$r_kwl[amount];
	$arr[16]=$r_pkn[amount]==""?0:$r_pkn[amount];
	$arr[17]=$r_pp3[amount]==""?0:$r_pp3[amount];
	$arr[18]=$r_kll[amount]==""?0:$r_kll[amount];
	$arr[25]=saldo_awal_ri_kredit_pusat($inp_cook,$inp_par);

	//Kredit
	$arr[22]=$arr["22.1"]+$arr["22.2"];						//penerimaan pengembalian kredit
	$arr[23]=$arr["23.1"]+$arr["23.2"];						//pengembalian kredit
	$arr[24]=$arr[22]-$arr[23];						//pemakain kredit
	$arr[26]=$arr[24]+$arr[25];						//Akumulasi kredit

	//Penerimaan
	$arr[5]=$arr["5.1"]+$arr["5.2"]+$arr["5.3"]+$arr["5.4"];                                //Pengembalian Provisi bg lc kmk
	$arr[2]=$arr[3]+$arr[4]+$arr[5];                                        //kewajiban wilayah
	$arr[6]=$arr[22];                                                      //penerimaan kredit
	$arr[7]=$arr[8]+$arr[9]+$arr[10];                                    //penerimaan pihak ke 3
	$arr[1]=$arr[2]+$arr[6]+$arr[7];                                      //penerimaan
	$arr[11]=$arr[1];                                                       //jumlah pemasukan

	//Pengeluaran
	$arr[14]=$arr["14.1"]+$arr["14.2"]+$arr["14.3"]+$arr["14.4"]+$arr["14.5"];                  //Wilayah lainnya
	$arr[15]=$arr[23];                                                      //pengembalian kredit
	$arr[12]=$arr[13]+$arr[14]+$arr[15]+$arr[16]+$arr[17]+$arr[18];//pengeluaran
#echo $arr[16];
	$arr[19]=$arr[12];                                                      //jumlah pengeluaran

	$arr[20]=$arr[1]-$arr[12];                                              //surplus
	$arr[21]=$arr[0]+$arr[20];                                              //saldo akhir kas bank

	return $arr;
}
/*
   function rumus_ri_cf_pusat($arr,$inp_par,$inp_cook){
   $sql_piu="select sum(nilai) as amount from tbl_ri_cf_proyek_pusat where date_part('month',tanggal)=$inp_par[cmb_bulan] and date_part('year',tanggal)=$inp_par[cmb_tahun] and minggu=$inp_par[minggu]";
   $res_piu=dbresult($sql_piu);
   $r_piu=pg_fetch_array($res_piu);

   $sql_pjk="select sum(nilai) as amount from tbl_ri_cf_pajak_pusat where date_part('month',tanggal)=$inp_par[cmb_bulan] and date_part('year',tanggal)=$inp_par[cmb_tahun] and minggu=$inp_par[minggu]";
   $res_pjk=dbresult($sql_pjk);
   $r_pjk=pg_fetch_array($res_pjk);


   $sql_div="select divisi_name,divisi_kode from tbl_divisi where divisi_kode not like '%.%' and divisi_kode!='$inp_cook[pusat_kode]' order by divisi_kode";
   $res_div=dbresult($sql_div);
   while($r_div=pg_fetch_array($res_div)){
   $arr[3]+=0;
   $arr[4]+=0;
   $arr[5]+=0;
   }

   $arr[0]=saldo_awal_ri_cf_pusat($inp_cook,$inp_par);

   $arr[2]=$arr[3]+$arr[4]+$arr[5];
   $arr[7]=$r_piu[amount]==""?0:$r_piu[amount];
   $arr[8]=$r_pjk[amount]==""?0:$r_pjk[amount];
   $arr[6]=$arr[7]+$arr[8];
   $arr[10]=$arr[11]+$arr[12]+$arr[13]+$arr[14]+$arr[15];	
   $arr[1]=$arr[16]=$arr[2]+$arr[6]+$arr[9]+$arr[10];	

   $arr[18]=$arr[19]+$arr[20]+$arr[21]+$arr[22]+$arr[23]+$arr[24]+$arr[25]+$arr[26]+$arr[27]+$arr[28]+$arr[29]+$arr[30];
   $arr[31]=$arr[32]+$arr[33]+$arr[34]+$arr[35]+$arr[36]+$arr[37]+$arr[38];
   $arr[17]=$arr[41]=$arr[18]+$arr[31]+$arr[39]+$arr[40];

   $arr[42]=$arr[1]-$arr[17];
   $arr[43]=$arr[44]+$arr[45];

   $arr[47]=$arr[0]+$arr[42];

   return $arr;
   }
 */
function rumus_pro_cf_pusat($arr,$inp_par,$inp_cook){
	$minggu = "minggu_".$inp_par[bln_rol]."_".$inp_par[rol];
	$rolling = "$inp_par[cmb_tahun_roll]-$inp_par[cmb_bulan_roll]-1";
	//dana talangan
	$sql_dat="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='4' and rolling='$rolling'";
	$res_dat=dbresult($sql_dat);
	$r_dat=pg_fetch_array($res_dat);
	//lain lain (kewajiban)
	$sql_wll="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='5.5' and rolling='$rolling'";
	$res_wll=dbresult($sql_wll);
	$r_wll=pg_fetch_array($res_wll);
	//piutang
	$sql_piu="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='8' and rolling='$rolling'";
	$res_piu=dbresult($sql_piu);
	$r_piu=pg_fetch_array($res_piu);
	//pajak
	$sql_pjk="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='9' and rolling='$rolling'";
	$res_pjk=dbresult($sql_pjk);
	$r_pjk=pg_fetch_array($res_pjk);
	//lain lain (penerimaan)
	$sql_tll="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='10' and rolling='$rolling'";
	$res_tll=dbresult($sql_tll);
	$r_tll=pg_fetch_array($res_tll);
	//kantor pusat
	$sql_kp="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='13' and rolling='$rolling'";
	$res_kp=dbresult($sql_kp);
	$r_kp=pg_fetch_array($res_kp);
	//wilayah
	$sql_wll="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='14.5' and rolling='$rolling'";
	$res_wll=dbresult($sql_wll);
	$r_kwl=pg_fetch_array($res_wll);
	//pembayaran kepada negara
	$sql_ppn="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='16' and rolling='$rolling'";
	$res_ppn=dbresult($sql_ppn);
	$r_pkn=pg_fetch_array($res_ppn);
	//pihak ketiga
	$sql_pp3="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='17' and rolling='$rolling'";
	$res_pp3=dbresult($sql_pp3);
	$r_pp3=pg_fetch_array($res_pp3);
	//lain lain pengeluaran
	$sql_kll="select sum(".$minggu.") as amount from tbl_detail_proyeksi where tahun='$inp_par[cmb_tahun]' and urutan_induk='18' and rolling='$rolling'";
	$res_kll=dbresult($sql_kll);
	$r_kll=pg_fetch_array($res_kll);

	//setoran kewajiban
	$sql_wil="select sum(".$minggu.") as amount from tbl_cf_proyeksi_divisi where divisi_kode not like '%.%' and divisi_kode!='$inp_cook[pusat_kode]' and urutan=17 and tahun=$inp_par[cmb_tahun] and rolling='$rolling'";
	$res_wil=dbresult($sql_wil);
	$r_wil=pg_fetch_array($res_wil);

	$arr[0]=saldo_awal_ri_cf_pusat($inp_cook,$inp_par);
	$arr[3]=$r_wil[amount]==""?0:$r_wil[amount];
	$arr[4]=$r_dat[amount]==""?0:$r_dat[amount];
	$arr["5.5"]=$r_wll[amount]==""?0:$r_wll[amount];
	$arr[8]=$r_piu[amount]==""?0:$r_piu[amount];
	$arr[9]=$r_pjk[amount]==""?0:$r_pjk[amount];
	$arr[10]=$r_tll[amount]==""?0:$r_tll[amount];
	$arr[13]=$r_kp[amount]==""?0:$r_kp[amount];
	$arr["14.5"]=$r_kwl[amount]==""?0:$r_kwl[amount];
	$arr[16]=$r_pkn[amount]==""?0:$r_pkn[amount];
	$arr[17]=$r_pp3[amount]==""?0:$r_pp3[amount];
	$arr[18]=$r_kll[amount]==""?0:$r_kll[amount];
	$arr[25]=saldo_awal_ri_kredit_pusat($inp_cook,$inp_par);

	//Kredit
	$arr[22]=$arr["22.1"]+$arr["22.2"];						//penerimaan pengembalian kredit
	$arr[23]=$arr["23.1"]+$arr["23.2"];						//pengembalian kredit
	$arr[24]=$arr[22]-$arr[23];						//pemakain kredit
	$arr[26]=$arr[24]+$arr[25];						//Akumulasi kredit

	//Penerimaan
	$arr[5]=$arr["5.1"]+$arr["5.2"]+$arr["5.3"]+$arr["5.4"];                                //Pengembalian Provisi bg lc kmk
	$arr[2]=$arr[3]+$arr[4]+$arr[5];                                        //kewajiban wilayah
	$arr[6]=$arr[22];                                                      //penerimaan kredit
	$arr[7]=$arr[8]+$arr[9]+$arr[10];                                    //penerimaan pihak ke 3
	$arr[1]=$arr[2]+$arr[6]+$arr[7];                                      //penerimaan
	$arr[11]=$arr[1];                                                       //jumlah pemasukan

	//Pengeluaran
	$arr[14]=$arr["14.1"]+$arr["14.2"]+$arr["14.3"]+$arr["14.4"]+$arr["14.5"];                  //Wilayah lainnya
	$arr[15]=$arr[23];                                                      //pengembalian kredit
	$arr[12]=$arr[13]+$arr[14]+$arr[15]+$arr[16]+$arr[17]+$arr[18];//pengeluaran
#echo $arr[16];
	$arr[19]=$arr[12];                                                      //jumlah pengeluaran

	$arr[20]=$arr[1]-$arr[12];                                              //surplus
	$arr[21]=$arr[0]+$arr[20];                                              //saldo akhir kas bank
	return $arr;
}

function rumus_racf_divisi($arr,$inp_par){
	global $inp_cook;

	$arr[0]=is_numeric($arr[0])?$arr[0]:0;
	$arr[1]=is_numeric($arr[1])?$arr[1]:0;
	$arr[2]=is_numeric($arr[2])?$arr[2]:0;
	$arr[3]=is_numeric($arr[3])?$arr[3]:0;
	$arr[6]=is_numeric($arr[6])?$arr[6]:0;
	$arr[8]=is_numeric($arr[8])?$arr[8]:0;
	$arr[9]=is_numeric($arr[9])?$arr[9]:0;
	$arr[14]=is_numeric($arr[14])?$arr[14]:0;
	$arr[15]=is_numeric($arr[15])?$arr[15]:0;
	$arr[17]=is_numeric($arr[17])?$arr[17]:0;
	$arr[28]=is_numeric($arr[28])?$arr[28]:0;
	$arr[29]=is_numeric($arr[29])?$arr[29]:0;
	$arr[12]=is_numeric($arr[12])?$arr[12]:0;
	$arr[18]=is_numeric($arr[18])?$arr[18]:0;
	$arr[21]=is_numeric($arr[21])?$arr[21]:0;
	//Pending
	$arr[22]=is_numeric($arr[22])?$arr[22]:0;
	$arr[24]=is_numeric($arr[24])?$arr[24]:0;
	$arr[25]=is_numeric($arr[25])?$arr[25]:0;
#$arr[26]=is_numeric($arr[26])?$arr[26]:0;
#$arr[27]=is_numeric($arr[27])?$arr[27]:0;

	//$arr[6]=0;
	//Pemasukan
	$arr[7]=$arr[8]+$arr[9];
	$arr[5]=$arr[6]+$arr[7];
	$arr[10]=$arr[6]+$arr[7];

	//Pengeluaran
	$arr[13]=$arr[14]+$arr[15];		//Pajak
	$arr[16]=$arr[17]+$arr[18];		//Kewajiban
	$arr[20]=$arr[21]+$arr[22];		// bank bunga
	$arr[23]=$arr[24]+$arr[25];		//provisi
	$arr[19]=$arr[20]+$arr[23];		//bb * pro
	$arr[11]=$arr[12]+$arr[13]+$arr[16]+$arr[19];
	$arr[26]=$arr[11]; # podo  karo $arr[11]?
		//selisih
		$arr[27]=$arr[4]+$arr[10]-$arr[26];

	$arr[31]=$arr[28]-$arr[29]+$arr[30];		//akumulasi kredit
	$arr[32]=$arr[27]+$arr[28]-$arr[29];		//saldo akhir

	return $arr;
}

function saldo_awal_proyek_rekening($inp_cook,$inp_par){
	//	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	//	$bulan=date("n",$tgl_sebelum);
	//	$tahun=date("Y",$tgl_sebelum);
	$sql="select sum(debet) as debet, sum(kredit) as kredit from tbl_rekening_proyek where tanggal<'$inp_par[cmb_tahun]-$inp_par[cmb_bulan]-1' and no_spk='$inp_cook[no_spk]'";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	$inp_par["debet"]=$r[debet];
	$inp_par["kredit"]=$r[kredit];
	$inp_par["saldo"]=$r[debet]-$r[kredit];
	return $inp_par;
}

function saldo_awal_rekening_divisi($inp_cook,$inp_par){
	//	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	//	$bulan=date("n",$tgl_sebelum);
	//	$tahun=date("Y",$tgl_sebelum);

	$sql="select sum(debet) as debet, sum(kredit) as kredit from tbl_rekening_divisi where tanggal<'$inp_par[cmb_tahun]-$inp_par[cmb_bulan]-1' and jenis='$inp_par[cmb_jenis]' and divisi_kode='$inp_cook[divisi_kode]'";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	$inp_par["debet"]=$r[debet];
	$inp_par["kredit"]=$r[kredit];
	$inp_par["saldo"]=$r[debet]-$r[kredit];
	return $inp_par;
}

function minggu($i,$jenis){
#for($i=1;$i<=12;$i++){
	$k++;
	$ming="minggu_$i";
	for($j=1;$j<=4;$j++){
		$ming.="_$j";
		$arr_minggu[$k]=$ming;
		$k++;
		$ming="minggu_$i";
	}
	$k++;
	if($jenis=="divisi"){
		$arr_minggu[$k]="bulan_$i";
	}
#}
	$str_minggu=implode(",",$arr_minggu);
	return $str_minggu;
}

function bulan($i){
#for($i=1;$i<=12;$i++){
	$k++;
	$ming="minggu_$i";
	for($j=1;$j<=4;$j++){
		$ming.="_$j";
		$arr_minggu[$k]=$ming;
		$k++;
		$ming="minggu_$i";
	}
	$k++;
#$arr_minggu[$k]="bulan_$i";
#}
	$str_minggu=implode("+",$arr_minggu);
	return $str_minggu;
}

function close(){
	echo "<script>
		window.opener.location.reload();
	window.close()
		</script>";
}

function saldo_awal_rekening_pusat($inp_cook,$inp_par){
	//	$tgl_sebelum=dateadd("m",-1,mktime(0,0,0,$inp_par[cmb_bulan],1,$inp_par[cmb_tahun]));
	//	$bulan=date("n",$tgl_sebelum);
	//	$tahun=date("Y",$tgl_sebelum);

	$sql="select sum(debet) as debet, sum(kredit) as kredit from tbl_rekening_divisi where tanggal<'$inp_par[cmb_tahun]-$inp_par[cmb_bulan]-1' and jenis='$inp_par[cmb_jenis]' and divisi_kode='$inp_cook[pusat_kode]'";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	$inp_par["debet"]=$r[debet];
	$inp_par["kredit"]=$r[kredit];
	$inp_par["saldo"]=$r[debet]-$r[kredit];
	return $inp_par;
}

function load_data_ra_per_proyek($tahun,$table,$tambahan="",$str_fld="",$var_loop=12){
	global $inp_cook;

	$j='jml';
	$sql="select a.*,b.no_spk,a.$j as bulan_0 $str_fld from tbl_cf_ra_proyek a,tbl_proyek b where a.no_spk = b.no_spk and a.tahun='$tahun' and b.divisi_kode='$inp_cook[divisi_kode]' $tambahan and date_part('year',b.mulai)<='$tahun' and (b.tanggal_close is null or date_part('year',b.tanggal_close)>='$tahun') order by b.no_spk,a.urutan";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		for($i=0;$i<=$var_loop;$i++){
			if(!cek_approve_ra_proyek($r[no_spk],$tahun)){
				$arr_cf["$r[no_spk]"][1][$i]=0;
				$arr_cf["$r[no_spk]"][2][$i]=0;
				$arr_cf["$r[no_spk]"][3][$i]=0;
				$arr_cf["$r[no_spk]"][4][$i]=0;
				$arr_cf["$r[no_spk]"][5][$i]=0;
				$arr_cf["$r[no_spk]"][6][$i]=0;
				$arr_cf["$r[no_spk]"][13][$i]=0;
				$arr_cf["$r[no_spk]"][22][$i]=0;
				$arr_cf["$r[no_spk]"][25][$i]=0;
				for($j=1;$j<=4;$j++){
					$tmp_field=$i."_".$j;
					$arr_cf["$r[no_spk]"][1][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][2][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][3][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][4][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][5][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][6][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][13][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][22][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][25][$tmp_field]=0;
				}
				//End dan kemuDIAN ...
			}else{
				if($r[urutan]==0){
					$temp=1;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==1){
					$temp=2;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==2){
					$temp=3;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==3){
					$temp=4;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==4){
					$temp=5;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif(($r[urutan]==6) or ($r[urutan]==7) or ($r[urutan]==8) or ($r[urutan]==9) or ($r[urutan]==12)){
					$temp=7;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif(($r[urutan]==26) or ($r[urutan]==28)  or ($r[urutan]==30)){
					$temp=13;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==29){
					$temp=22;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==32){
					$temp=25;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}
				//$arr_cf["$r[no_spk]"][$temp][0]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;

				for($j=1;$j<=4;$j++){
					$tmp_nama="minggu_".$i."_".$j;
					$tmp_field=$i."_".$j;
					if($r[urutan]==0){
						$temp=1;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
#echo $arr_cf["K.06.002.00.004"][1]["4_1"]." as<br>";
#echo $r[no_spk]." / ".$temp." / ".$tmp_field." / ".$r["$tmp_nama"]." <br>";
					}elseif($r[urutan]==1){
						$temp=2;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==2){
						$temp=3;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==3){
						$temp=4;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==4){
						$temp=5;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif(($r[urutan]==6) or ($r[urutan]==7) or ($r[urutan]==8) or ($r[urutan]==9) or ($r[urutan]==12)){
						$temp=7;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif(($r[urutan]==26) or ($r[urutan]==28) or ($r[urutan]==30)){
						$temp=13;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]+=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==29){
						$temp=22;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]+=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==32){
						$temp=25;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]+=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}
				}
			}// Penutup cek approve, DIE ...
		}
	}
#	sort($arr_cf);
#	echo "<pre>";	
#	print_r($arr_cf);
#	echo "</pre>";	
	return $arr_cf;
}
function load_data_per_proyek($tahun,$table,$tambahan="",$str_fld="",$var_loop=12,$bproyeksi=""){
	global $inp_cook;

	if($table=='tbl_cf_proyeksi_proyek'){
		$j='jml_sd';
	}else{
		$j='jml';
	}
	$sql="select a.*,b.no_spk,a.$j as bulan_0 $str_fld from $table a,tbl_proyek b where a.no_spk = b.no_spk and a.tahun='$tahun' and b.divisi_kode='$inp_cook[divisi_kode]' $tambahan and date_part('year',b.mulai)<='$tahun' and (b.tanggal_close is null or date_part('year',b.tanggal_close)>='$tahun') order by b.no_spk,a.urutan";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		for($i=0;$i<=$var_loop;$i++){
#$str_check_approve=cek_approve_proyek($r[no_spk],$i,$tahun)?"true":"false";
			//Start cek approve dan isi arr dengan 0
			if(trim($table)=="tbl_cf_proyeksi_proyek"){
				$sbcp=$bproyeksi;
			}else{
				$sbcp=$i;
			}
			if(!cek_approve_proyek($r[no_spk],$sbcp,$tahun)){
				$arr_cf["$r[no_spk]"][1][$i]=0;
				$arr_cf["$r[no_spk]"][2][$i]=0;
				$arr_cf["$r[no_spk]"][3][$i]=0;
				$arr_cf["$r[no_spk]"][4][$i]=0;
				$arr_cf["$r[no_spk]"][5][$i]=0;
				$arr_cf["$r[no_spk]"][6][$i]=0;
				$arr_cf["$r[no_spk]"][13][$i]=0;
				$arr_cf["$r[no_spk]"][22][$i]=0;
				$arr_cf["$r[no_spk]"][25][$i]=0;
				for($j=1;$j<=4;$j++){
					$tmp_field=$i."_".$j;
					$arr_cf["$r[no_spk]"][1][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][2][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][3][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][4][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][5][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][6][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][13][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][22][$tmp_field]=0;
					$arr_cf["$r[no_spk]"][25][$tmp_field]=0;
				}
				//End dan kemuDIAN ...
			}else{
				if($r[urutan]==0){
					$temp=1;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==1){
					$temp=2;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==2){
					$temp=3;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==3){
					$temp=4;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==4){
					$temp=5;
					$arr_cf["$r[no_spk]"][$temp][$i]=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif(($r[urutan]==6) or ($r[urutan]==7) or ($r[urutan]==8) or ($r[urutan]==9) or ($r[urutan]==12)){
					$temp=7;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif(($r[urutan]==26) or ($r[urutan]==28)  or ($r[urutan]==30)){
					$temp=13;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==29){
					$temp=22;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}elseif($r[urutan]==32){
					$temp=25;
					$arr_cf["$r[no_spk]"][$temp][$i]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;
				}
#if($i=='6'){
#	$temp=7;
#	echo $arr_cf["$r[no_spk]"][7][6]."<br />";
#}	
				//$arr_cf["$r[no_spk]"][$temp][0]+=is_numeric($r["bulan_$i"])?$r["bulan_$i"]:0;

				for($j=1;$j<=4;$j++){
					$tmp_nama="minggu_".$i."_".$j;
					$tmp_field=$i."_".$j;
					if($r[urutan]==0){
						$temp=1;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
#echo $arr_cf["K.06.002.00.004"][1]["4_1"]." as<br>";
#echo $r[no_spk]." / ".$temp." / ".$tmp_field." / ".$r["$tmp_nama"]." <br>";
					}elseif($r[urutan]==1){
						$temp=2;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==2){
						$temp=3;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==3){
						$temp=4;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==4){
						$temp=5;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif(($r[urutan]==6) or ($r[urutan]==7) or ($r[urutan]==8) or ($r[urutan]==9) or ($r[urutan]==12)){
						$temp=7;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]+=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif(($r[urutan]==26) or ($r[urutan]==28) or ($r[urutan]==30)){
						$temp=13;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]+=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==29){
						$temp=22;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]+=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}elseif($r[urutan]==32){
						$temp=25;
						$arr_cf["$r[no_spk]"][$temp][$tmp_field]+=is_numeric($r["$tmp_nama"])?$r["$tmp_nama"]:0;
					}
				}
			}// Penutup cek approve, DIE ...
		}
	}
#	sort($arr_cf);
#	echo "<pre>";	
#	print_r($arr_cf);
#	echo "</pre>";	
	return $arr_cf;
}
//untuk cetak array, untuk debug doang...
function ctk_r($nilai){
	echo "<pre>";
	print_r($nilai);
	echo "</pre>";
}
function nilai_sql($sql){
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	return $r;

}

function format_tanggal($tgl){
	$arr=split("-",$tgl);
	return "$arr[2]-$arr[1]-$arr[0]";
}

function load_index($no_spk,$bln,$thn){

	$sql_proyek="select mulai from tbl_proyek where no_spk='$no_spk'";
	$res_proyek=dbresult($sql_proyek);
	$r_proyek=pg_fetch_array($res_proyek);
	$mulai=$r_proyek[mulai];
	$tgl_ssd=dateadd("m",1,mktime(0,0,0,$bln,1,$thn));
	$tgl_sbl=split("-",date("Y-m-j",dateadd("m",-1,mktime(0,0,0,$bln,1,$thn))));
	$tgl_sbl[1] = intval($tgl_sbl[1]);
	if($mulai<$tgl_ssd){
		$sql_proyeksi="select bulan_$tgl_sbl[1],urutan from tbl_cf_ra_proyek where no_spk='$no_spk' and tahun='$tgl_sbl[0]' and urutan in (2,13,40)";
	}else{
		$sql_proyeksi="select bulan_$tgl_sbl[1],urutan from tbl_cf_proyeksi_proyek where no_spk='$no_spk' and rolling='$tgl_sbl[0]-$tgl_sbl[1]-1' and tahun='$tgl_sbl[0]' and urutan in (2,13,40)";
	}
#echo $sql_proyeksi;
	$res_proyeksi=dbresult($sql_proyeksi);
	while($r=pg_fetch_array($res_proyeksi)){
		$arr_index[proyeksi]["$r[urutan]"]=$r["bulan_$tgl_sbl[1]"];
	}

	$sql_realisasi="select bulan_$tgl_sbl[1],urutan from tbl_cf_ri_proyek where no_spk='$no_spk' and tahun='$tgl_sbl[0]' and urutan in (2,13,40)";
	$res_realisasi=dbresult($sql_realisasi);
	while($r=pg_fetch_array($res_realisasi)){
		$arr_index[realisasi]["$r[urutan]"]=$r["bulan_$tgl_sbl[1]"];
	}

	$sql_biaya="select ra_bulan_$tgl_sbl[1],ri_bulan_$tgl_sbl[1] from tbl_ra_ri_biaya_proyek where no_spk='$no_spk' and tahun='$tgl_sbl[0]'";
	$res_biaya=dbresult($sql_biaya);
	while($r=pg_fetch_array($res_biaya)){
		$arr_index[biaya][rencana]=$r["ra_bulan_$tgl_sbl[1]"];
		$arr_index[biaya][realisasi]=$r["ri_bulan_$tgl_sbl[1]"];
	}


	$sql_koef="select * from tbl_koefisien";
	$res_koef=dbresult($sql_koef);
	while($r=pg_fetch_array($res_koef)){
		$arr_index[koefisien][a]=$r["koef_pendapatan"];
		$arr_index[koefisien][b]=$r["koef_biaya"];
		$arr_index[koefisien][c]=$r["koef_cash_in"];
		$arr_index[koefisien][d]=$r["koef_cash_out"];
	}
	$p0=$arr_index[proyeksi][2];
	$p1=$arr_index[realisasi][2];
	$b0=$arr_index[biaya][rencana];
	$b1=$arr_index[biaya][realisasi];
	$ci0=$arr_index[proyeksi][13];
	$ci1=$arr_index[realisasi][13];
	$co0=$arr_index[proyeksi][40];
	$co1=$arr_index[realisasi][40];
	$a=$arr_index[koefisien][a];
	$b=$arr_index[koefisien][b];
	$c=$arr_index[koefisien][c];
	$d=$arr_index[koefisien][d];

	$arr_index[index][hasil]=((($p0=="" or $p0==0)?0:$p1/$p0) * $a/100 ) + ((($b0=="" or $b0==0)?0:$b1/$b0) * $b/100 ) + ((($ci0==0 or $ci0=="")?0:$ci1/$ci0) * $c/100 ) + ((($co0=="" or $co0==0)?0:$co1/$co0)* $d/100 );
	/*
	   echo "<pre>";
	   print_r($arr_index);
	   print_r($tgl_sbl);
	   echo "</pre>";
	 */
	return $arr_index[index][hasil];
}

function array_otorisasi($nospk,$tahun){
	$sql="select date_part('month',tanggal) as bulan,* from tbl_approve_proyek where no_spk='$nospk' and date_part('year',tanggal)='$tahun'";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_app[$nospk][$tahun]["$r[bulan]"]=true;
	}
	return $arr_app;
}
function array_otorisasi_divisi($divisi_kode,$tahun){
	$sql="select date_part('month',tanggal) as bulan,* from tbl_approve_proyek where divisi_kode='$divisi_kode' and date_part('year',tanggal)='$tahun'";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arr_app[$divisi_kode][$tahun]["$r[bulan]"]=true;
	}
	return $arr_app;
}

function nama_bulan($bulan){
	$arr=array ("1" => "Januari","2" => "Februari","3" => "Maret","4" => "April","5" => "Mei","6" => "Juni","7" => "Juli", "8" => "Agustus","9" => "September","01" => "Januari","02" => "Februari","03" => "Maret","04" => "April","05" => "Mei","06" => "Juni","07" => "Juli", "08" => "Agustus","09" => "September","10" => "Oktober","11" => "November","12" => "Desember");
	$hasil=$arr["$bulan"];
	return $hasil;
}

function pindah_tarif($jenis){
	$nama_file="tarif-$jenis.js";
	$sql="select * from tbl_dm_tarif_$jenis order by kode_tarif_$jenis";
	array_tarif($sql,$nama_file,$jenis);
}

function array_tarif($sql,$nama_file,$jenis){
	$nama_array="arr_persen";
	$res=dbresult($sql);
	if(!$tulisfile=fopen($nama_file,'w+')){
		print("Buka File $nama_file Gagal");
		$hasil=false;
	}else{
		$isi_file="var $nama_array=new Array(); \n";
		while($r=pg_fetch_array($res)){
			$key=$r["kode_tarif_$jenis"];
			$isi_file.=$nama_array."['tarif$key']='$r[nilai_tarif]'; \n";
			$isi_file.=$nama_array."['netto$key']='$r[nilai_netto]'; \n";
		}
	}
	if(!fwrite($tulisfile,$isi_file)){
		print("Penulisan File $nama_file Gagal");
		$hasil=false;
	}else{
		fclose($tulisfile);
	}
	return $hasil;
}

function persen($jenis){
	$sql="select * from tbl_dm_tarif_$jenis";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$kode=$r["kode_tarif_$jenis"];
		$hasil["tarif"][$kode]=$r[nilai_tarif];
		$hasil["netto"][$kode]=$r[nilai_netto];
	}
	return $hasil;
}

function no_bukti($jenis){
	global $ckode_cabang;
	$tahun=date('y');

	$sql="select * from tbl_dm_format_dokumen where jenis_pajak='$jenis'";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);

	$kode=$r[fps_prefik].$r[fps_sufiks]."/".$ckode_cabang."/".$tahun."/";

	$bukti=str_replace("$kode","",cari_nilai("select no_bukti from qry_no_bukti_".strtolower($jenis)." where no_bukti like '$kode%' order by no_bukti desc"));

	$angka=$bukti+1;

	$digit=strlen($r[fps_digit]);

	for($i=1;$i<=$digit-strlen($angka);$i++){
		$nol=$nol."0";
	}

	$hasil=$kode.$nol.$angka;

	return $hasil;
}


function no_faktur($jenis_transaksi="",$status_detail=""){
	global $ckode_cabang;
	$tahun=date('y');
	$jenis_transaksi=($jenis_transaksi=="" or $jenis_transaksi=="0")?1:2;
	$status_detail=$status_detail==""?0:($status_detail-1);

	$kode=$ckode_cabang."-"."0".$jenis_transaksi.$status_detail."-".$tahun."-";
	$faktur=str_replace("$kode","",cari_nilai("select no_faktur_keluaran from tbl_fp_faktur_keluaran where no_faktur_keluaran like '$kode%' order by no_faktur_keluaran desc"));
	$angka=$faktur+1;

	$digit=8;

	for($i=1;$i<=$digit-strlen($angka);$i++){
		$nol=$nol."0";
	}

	$hasil=$kode.$nol.$angka;
	return $hasil;
}

function no_faktur_eghie($jenis_transaksi="", $status_detail="",$kd1akhir="",$thn, $kode_divisi){
	global $ckode_cabang;
	$kode_cabang = cari_nilai("select kode_cabang from tbl_dm_atribut_perusahaan where divisi_kode='$kode_divisi'");
//        if ($dok_transaksi==5){
//            $kd1akhir = 1;
//        }else{
//            $kd1akhir = 0;
//        }
        $kd1 = "0".$status_detail.$kd1akhir;
        $hasil= $kd1."-".$kode_cabang;
	return $hasil;
}


function kode_pembetulan($jenis,$divisi,$tahun,$bulan=""){
	$bulan=$bulan==""?"":"and date_part('month',tgl_pajak)=$bulan";
	$sql="select nomor_pembetulan from tbl_pembetulan where jenis_pasal='$jenis' and divisi_kode='$divisi' and date_part('year',tgl_pajak)=$tahun $bulan order by nomor_pembetulan desc";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	$hasil=$r[nomor_pembetulan]==""?0:$r[nomor_pembetulan];
	return $hasil;
}

function disabled(){
	global $permisi;
	$hasil=$permisi["disabled"]=="izin"?"":"readonly";
	return $hasil;
}

function divisi($select,$name){
	global $cdivisi_kode;
	$cdivisi=$cdivisi_kode;
	$divisi=$cdivisi=="KP"?"":"$select ($name='$cdivisi' or $name like '$cdivisi.%')";
	return $divisi;
}

function arr_nama($variabel1,$variabel2,$variabel3){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$sql="select $variabel1,$variabel2 from $variabel3 order by $variabel1 ";

	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){

		$acc=$r[$variabel1];
		$arr_temp[$acc]["nama"]=$r[$variabel2];
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;

}

function arr_sum_ak($bulan,$tahun,$panjang=5){
	global $cdivisi_account;
	global $tanda_account,$tanda_laba;
	$tanda_lb=tanda_lb(5);
	$bulan=$bulan=="01"?1:$bulan;
	$bulan=$bulan=="02"?2:$bulan;
	$bulan=$bulan=="03"?3:$bulan;
	$bulan=$bulan=="04"?4:$bulan;
	$bulan=$bulan=="05"?5:$bulan;
	$bulan=$bulan=="06"?6:$bulan;
	$bulan=$bulan=="07"?7:$bulan;
	$bulan=$bulan=="08"?8:$bulan;
	$bulan=$bulan=="09"?9:$bulan;
	$d_ini="dt_".$bulan;
	$k_ini="kt_".$bulan;
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	for($i=1;$i<=($bulan-1);$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	//$bulan=trim(str_replace("0","",$bulan));	
	$str_debet=$str_debet_lalu.$str_debet_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_ini;
	$sql="select account_kode,sum(dsll) as debet_lalu,sum(ksll) as kredit_lalu,sum($d_ini) as debet_bln_ini,sum($k_ini) as kredit_bln_ini,sum($str_debet_ini) as debet_sampai_ini,sum($str_kredit_ini) as kredit_sampai_ini,sum($str_debet) as jml_debet_total,sum($str_kredit) as jml_kredit_total from tbl_summary_account where tahun='$tahun' and account_kode<>'' ".sql_divisi_kode()." group by tahun,account_kode";
	//echo $sql."<br>";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$arr_temp[$acc]["debet_lalu"]=strpos($tanda_lb,$acc)>0?$r[debet_lalu]:$r[kredit_lalu];
		$arr_temp[$acc]["kredit_lalu"]=strpos($tanda_lb,$acc)>0?$r[kredit_lalu]:$r[debet_lalu];
		$arr_temp[$acc]["debet_bln_ini"]=strpos($tanda_lb,$acc)>0?$r[debet_bln_ini]:$r[kredit_bln_ini];
		$arr_temp[$acc]["kredit_bln_ini"]=strpos($tanda_lb,$acc)>0?$r[kredit_bln_ini]:$r[debet_bln_ini];
		$arr_temp[$acc]["debet_sampai_ini"]=strpos($tanda_lb,$acc)>0?$r[debet_sampai_ini]:$r[kredit_sampai_ini];
		$arr_temp[$acc]["kredit_sampai_ini"]=strpos($tanda_lb,$acc)>0?$r[kredit_sampai_ini]:$r[debet_sampai_ini];
		//saldo lalu
		$arr_temp[$acc]["saldo_lalu"]=strpos($tanda_lb,$acc)>0?$arr_temp[$acc]["debet_lalu"]-$arr_temp[$acc]["kredit_lalu"]:$arr_temp[$acc]["kredit_lalu"]-$arr_temp[$acc]["debet_lalu"];
		//saldo bulan ini 
		$arr_temp[$acc]["saldo_bln_ini"]=strpos($tanda_lb,$acc)>0?$arr_temp[$acc]["debet_bln_ini"]-$arr_temp[$acc]["kredit_bln_ini"]:$arr_temp[$acc]["kredit_bln_ini"]-$arr_temp[$acc]["debet_bln_ini"];
		//saldo sampai bulan ini 
		$arr_temp[$acc]["saldo_sampai_ini"]=strpos($tanda_lb,$acc)>0?$arr_temp[$acc]["debet_sampai_ini"]-$arr_temp[$acc]["kredit_sampai_ini"]:$arr_temp[$acc]["kredit_sampai_ini"]-$arr_temp[$acc]["debet_sampai_ini"];
		//saldo total 
		$arr_temp[$acc]["saldo_total"]=strpos($tanda_lb,$acc)>0?$arr_temp[$acc]["jml_debet_total"]-$arr_temp[$acc]["jml_kredit_total"]:$arr_temp[$acc]["jml_kredit_total"]-$arr_temp[$acc]["jml_debet_total"];
	}
	return $arr_temp;

}
/*
function arr_summary_account($bulan,$tahun,$panjang=5,$sql_tambahan=""){
	global $cdivisi_account,$cnospk;
	global $tanda_account;
	if($cnospk=="0000000000" || $cnospk==""){
		$sql_cnospk="";
	}else{
		$sql_cnospk=" and kode_proyek='$cnospk'";
	}
	$tanda_lb=tanda_laba(2);
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	for($i=1;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	$bulan=trim(str_replace("0","",$bulan));	
	$str_debet=$str_debet_lalu.$str_debet_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_ini;

	//$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";

	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' and mu='RP' and account_kode<>'' $sql_tambahan $sql_cnospk ".sql_divisi_kode2()." group by tahun,substr(account_kode,1,$panjang)";

	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){

		$acc=$r[account_kode];
		$kode_awal=substr($acc,0,$panjang);
		$sub_kode_awal=substr($acc,0,2);
		echo $sub_kode_awal." OK<br>";
		$saldo_lalu=strpos($tanda_lb,$sub_kode_awal)>0?$r[jml_kredit_lalu]-$r[jml_debet_lalu]:$r[jml_debet_lalu]-$r[jml_kredit_lalu];
		$saldo_ini=strpos($tanda_lb,$sub_kode_awal)>0?$r[jml_kredit_ini]-$r[jml_debet_ini]:$r[jml_debet_ini]-$r[jml_kredit_ini];
		//$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini];
		$saldo=strpos($tanda_lb,$sub_kode_awal)>0?($r[jml_kredit]-$r[jml_debet]):($r[jml_debet]-$r[jml_kredit]);
		$arr_temp[$acc]["saldo"]=$saldo==""?0:$saldo;
		$arr_temp[$acc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp[$acc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp[$acc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp[$acc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp[$acc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp[$acc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp[$acc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp[$acc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
	}
	 
	   //echo "<pre>";
	   //print_r($arr_temp);
	   //echo "</pre>";
	return $arr_temp;
}
*/

function arr_summary_account($bulan,$tahun,$panjang=5,$sql_tambahan="",$ada_proyek="y",$data_divisi_kode=""){
	global $cdivisi_account,$cnospk;
	global $tanda_account,$cdivisi_kode;
	if($cnospk=="0000000000" || $cnospk==""){
		$sql_cnospk="";
	}else{
		$sql_cnospk=" and kode_proyek='$cnospk'";
	}
	//$tanda_lb=tanda_lb();
	$data_divisi_kode=$data_divisi_kode==""?$cdivisi_kode:$data_divisi_kode;
	$tanda_lb=$panjang==5?tanda_lb():tanda_lb(2);
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	for($i=1;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	//$bulan=trim(str_replace("0","",$bulan));
	$bulan=intval($bulan);
	$str_debet=$str_debet_lalu.$str_debet_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_ini;

	//$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";
	$tambah_kode_proyek=$ada_proyek=="y"?",kode_proyek":"";
	$order_tambah_kode_proyek=$ada_proyek=="y"?",kode_proyek desc":"";
	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini $tambah_kode_proyek from tbl_summary_account where tahun='$tahun' and mu='RP' and account_kode<>''$sql_tambahan $sql_cnospk and data_divisi_kode='$data_divisi_kode' and length(kode_proyek)=10 group by tahun,substr(account_kode,1,$panjang) $tambah_kode_proyek order by substr(account_kode,1,$panjang) $order_tambah_kode_proyek ";

	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$kode_awal=substr($acc,0,$panjang);
		$kode_proyek=$r[kode_proyek];
		$sub_kode_awal=substr($acc,0,2);
		$saldo_lalu=strpos($tanda_lb,$sub_kode_awal)>0?$r[jml_kredit_lalu]-$r[jml_debet_lalu]:$r[jml_debet_lalu]-$r[jml_kredit_lalu];
		$saldo_ini=strpos($tanda_lb,$sub_kode_awal)>0?$r[jml_kredit_ini]-$r[jml_debet_ini]:$r[jml_debet_ini]-$r[jml_kredit_ini];
		//$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini];
		$saldo=strpos($tanda_lb,$sub_kode_awal)>0?($r[jml_kredit]-$r[jml_debet]):($r[jml_debet]-$r[jml_kredit]);
		if($ada_proyek=="y"){
			if(substr($r[account_kode],0,3)=="113"){
				if(substr($r[account_kode],0,4)=='1139'){
					if($r[account_kode]=='11391' or $r[account_kode]=='11392' or $r[account_kode]=='11393' or $r[account_kode]=='11394' or $r[account_kode]=='11395'){
						$jenis="ragu";
					}else{
						$jenis="penyisihan";
					}
				}else{
					if(substr($r[account_kode],0,4)=='1133'){
						$jenis="kerjasama hub. ist";
					}elseif(substr($r[account_kode],0,4)=='1131' or substr($r[account_kode],0,4)=='1132' or substr($r[account_kode],0,4)=='1134'){
						$jenis="usaha hub. ist.";
					}elseif(substr($r[account_kode],0,4)=='1137'){
						$jenis="kerjasama pihak ke-3";
					}else{
						if($kode_proyek=="0000000000"){
							//echo $r[account_kode]." -- $kode_proyek --> $saldo<br>";
						}
						$jenis="usaha pihak ke-3";
					}
				}
			}elseif(substr($r[account_kode],0,3)=='215' or substr($r[account_kode],0,3)=='222'){
				$jenis="uangmuka";
			}elseif(substr($r[account_kode],0,3)=='114'){
				$jenis="retensi";
			}elseif(substr($r[account_kode],0,3)=='115'){
				$jenis="prestasi";
			}elseif(substr($r[account_kode],0,3)=='211'){
				if(substr($r[account_kode],0,4)=="2111" || substr($r[account_kode],0,4)=="2112" || substr($r[account_kode],0,4)=="2113"){
					$jenis="usaha hub. ist.";
				}else{
					$jenis="usaha pihak ke-3";
				}
			}
			$arr_temp[$kode_proyek][$jenis]["saldo"]+=$saldo==""?0:$saldo;
			$arr_temp[$kode_proyek][$jenis]["debet"]+=$r[jml_debet]==""?0:$r[jml_debet];
			$arr_temp[$kode_proyek][$jenis]["kredit"]+=$r[jml_kredit]==""?0:$r[jml_kredit];;
			$arr_temp[$kode_proyek][$jenis]["debet_lalu"]+=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
			$arr_temp[$kode_proyek][$jenis]["kredit_lalu"]+=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
			$arr_temp[$kode_proyek][$jenis]["saldo_lalu"]+=$saldo_lalu==""?0:$saldo_lalu;
			$arr_temp[$kode_proyek][$jenis]["debet_ini"]+=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
			$arr_temp[$kode_proyek][$jenis]["kredit_ini"]+=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
			$arr_temp[$kode_proyek][$jenis]["saldo_ini"]+=$saldo_ini==""?0:$saldo_ini;
			$arr_temp[$kode_proyek][$jenis]["jenis"]=$jenis;

		}else{
			$arr_temp[$acc]["saldo"]=$saldo==""?0:$saldo;
			$arr_temp[$acc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
			$arr_temp[$acc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
			$arr_temp[$acc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
			$arr_temp[$acc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
			$arr_temp[$acc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
			$arr_temp[$acc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
			$arr_temp[$acc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
			$arr_temp[$acc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
		}
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;
}

function arr_summary_biaya_pekerjaan($bulan,$tahun,$panjang=4, $sql_tambahan){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$sql="select substr(account_kode,1,$panjang) as account_kode,proyek,sum(case when jenis='1' then jumlah else 0-jumlah end) as jumlah from tbl_debet_kredit where date_part('month',periode_posting)<=$bulan and tahun='$tahun' and mu='RP' and account_kode like '5%' $sql_tambahan ".sql_divisi_kode2()." group by tahun,substr(account_kode,1,$panjang),proyek order by account_kode";
	//	echo $sql."<br>";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$proyek=$r[proyek];
		$kode_awal=substr($acc,0,$panjang);
		$arr_temp[$acc][$proyek]=$r[jumlah]==""?0:$r[jumlah];;
		//$arr_temp[$acc][$proyek]=$jenis==1?$r[jumlah]:0-$r[jumlah];
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	if($inp_par[jenis_laporan]=="non_audit" || $inp_par[jenis_laporan]==""){
		//$sql="select account_kode,sum(nilai) as nilai,tahun from tbl_debet_kredit where tahun='$tahun' and date_part('month',periode_posting)='$bulan' and account_kode<>'' $sql_divisi $sql_cnospk group by tahun,account_kode";
 		$sqla="select substr((case when substr(account_kode,1,1)<'3' then account_kode else '31511' end),1,$panjang) as account_kode,sum(case when jenis='1' then nilai else 0-nilai end) as nilai,tahun from tbl_debet_kredit where tahun='$tahun' and no_transaksi like '%AD%' and account_kode<>'' $sql_divisi $sql_cnospk group by tahun,substr((case when substr(account_kode,1,1)<'3' then account_kode else '31511' end),1,$panjang)";
		$resa=dbresult($sqla);
		while($ra=pg_fetch_array($resa)){
			$acca=$ra[account_kode];
			$sub_acca=substr($acca,0,$panjang);
			$sub2_acca=substr($ra[account_kode],0,2);

			$arr_temp[$sub_acca]["nilai_audit"]+=strpos($tanda_lb,$sub2_acca)>0?$ra[nilai]:0-$ra[nilai];
			$arr_temp[$sub_acca]["sd_ini"]=$arr_temp[$sub_acca]["sd_ini"]-$arr_temp[$sub_acca]["nilai_audit"];
			$arr_temp[$sub_acca]["sd_lalu"]=$arr_temp[$sub_acca]["sd_lalu"]-$arr_temp[$sub_acca]["nilai_audit"];
		}	
	}
	return $arr_temp;

}

function arr_summary_buku_tambahan_proyek($proyek="",$tahun,$panjang=4){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	if($proyek==""){
		$strproyek="";
	}else{
		$strproyek="and proyek like '$proyek%'";
	}
	$sql="select substr(account_kode,1,$panjang) as account_kode,key_lain,debet_kredit_id,jenis,sum(jumlah) as jumlah from tbl_debet_kredit where tahun='$tahun' ".sql_divisi_kode2()." $strproyek group by tahun,substr(account_kode,1,$panjang),key_lain,debet_kredit_id,jenis order by account_kode,length(debet_kredit_id),debet_kredit_id";

	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$key_lain=$r[key_lain];
		$jenis=$r[jenis];
		$kode_awal=substr($acc,0,4);
		$debet_kredit_id=$r[debet_kredit_id];
		$arr_temp[$acc][$key_lain][$debet_kredit_id]=$r[jumlah]==""?0:$r[jumlah];;
		$arr_temp[$acc][$key_lain][$debet_kredit_id]=$jenis==1?$r[jumlah]:0-$r[jumlah];
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;

}

function sql_account($kode_account){
	$arr_x=split(",",$kode_account);
	while(list($kunci,$nilai)=each($arr_x)){
		$txt.=" account_kode like('%.$nilai.%') or ";
	}
	if($txt<>""){
		$txt=substr($txt,0,strlen($txt)-4);
		$txt=" and ($txt)";
	}
	return $txt;
}
function arr_sum_acc_18s($bulan,$tahun,$panjang=12){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	for($i=1;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	$bulan=trim(str_replace("0","",$bulan));	
	$str_debet=$str_debet_lalu.$str_debet_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_ini;
	//	$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";
	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' ".sql_account($cdivisi_account)." and account_kode like '18%.%' and account_kode like '%.%%%0' group by substr(account_kode,1,$panjang) order by substr(account_kode,1,$panjang)";
	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$kode_awal=substr($acc,0,3);
		$saldo_lalu=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_lalu]-$r[jml_debet_lalu]:$r[jml_debet_lalu]-$r[jml_kredit_lalu];
		$saldo_ini=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_ini]-$r[jml_debet_ini]:$r[jml_debet_ini]-$r[jml_kredit_ini];
		//echo "$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini] <br>";

		$saldo1=strpos($tanda_account,$kode_awal)>0?($r[jml_kredit]-$r[jml_debet]):($r[jml_debet]-$r[jml_kredit]);
		$arr_temp1[$acc]["saldo"]=$saldo1==""?0:$saldo1;
		$arr_temp1[$acc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp1[$acc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp1[$acc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp1[$acc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp1[$acc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp1[$acc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp1[$acc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp1[$acc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
	}
	$sql2="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' ".sql_account($cdivisi_account)." and account_kode like '18%.%' and account_kode not like '%.%%%0' group by substr(account_kode,1,$panjang) order by substr(account_kode,1,$panjang)";
	$res2=dbresult($sql2);//echo $sql;
	//echo $sql2."<br>";
	while($r2=pg_fetch_array($res2)){
		$acc2=$r2[account_kode];
		$kode_awal2=substr($acc2,0,3);
		$saldo_lalu2=strpos($tanda_account,$kode_awal2)>0?$r2[jml_kredit_lalu]-$r2[jml_debet_lalu]:$r2[jml_debet_lalu]-$r2[jml_kredit_lalu];
		$saldo_ini2=strpos($tanda_account,$kode_awal2)>0?$r2[jml_kredit_ini]-$r2[jml_debet_ini]:$r2[jml_debet_ini]-$r2[jml_kredit_ini];
		$saldo2=strpos($tanda_account,$kode_awal2)>0?($r2[jml_kredit]-$r2[jml_debet]):($r2[jml_debet]-$r2[jml_kredit]);
		$arr_temp2[$acc2]["saldo"]=$saldo2==""?0:$saldo2;
		$arr_temp2[$acc2]["debet"]=$r2[jml_debet]==""?0:$r2[jml_debet];
		$arr_temp2[$acc2]["kredit"]=$r2[jml_kredit]==""?0:$r2[jml_kredit];
		$arr_temp2[$acc2]["debet_lalu"]=$r2[jml_debet_lalu]==""?0:$r2[jml_debet_lalu];
		$arr_temp2[$acc2]["kredit_lalu"]=$r2[jml_kredit_lalu]==""?0:$r2[jml_kredit_lalu];;
		$arr_temp2[$acc2]["saldo_lalu"]=$saldo_lalu2==""?0:$saldo_lalu2;
		$arr_temp2[$acc2]["debet_ini"]=$r2[jml_debet_ini]==""?0:$r2[jml_debet_ini];
		$arr_temp2[$acc2]["kredit_ini"]=$r2[jml_kredit_ini]==""?0:$r2[jml_kredit_ini];;
		$arr_temp2[$acc2]["saldo_ini"]=$saldo_ini2==""?0:$saldo_ini2;
	}
	$sql3="select account_kode from tbl_summary_account where account_kode like '18%' ".sql_account($cdivisi_account)." group by account_kode order by account_kode";
	$res3=dbresult($sql3);//echo $sql;
	//echo $sql3."<br>";
	while($r3=pg_fetch_array($res3)){
		$acc3=$r3[account_kode];
		$kode_akhir=substr($acc,-1,1);
		$sub_acc3=substr($acc3,0,3);
		$arr_temp[$acc3]["saldo"]=$arr_temp1[$acc3]["saldo"];
		$arr_temp[$acc3]["debet"]=$arr_temp1[$acc3]["debet"]-$arr_temp2[$acc3]["debet"];
		$arr_temp[$acc3]["kredit"]=$arr_temp2[$acc3]["debet"]-$arr_temp2[$acc3]["kredit"];
	}
	/*	echo "<pre>";
		print_r($arr_temp1);
		echo "</pre>";
	 */	
	return $arr_temp;

}

function sql_divisi_kode(){
	global $cdivisi_kode;
	return " and (data_divisi_kode='$cdivisi_kode' or data_divisi_kode like '$cdivisi_kode.%')";
}

function sql_divisi_kode2(){
	global $cdivisi_kode;
	return " and (data_divisi_kode='$cdivisi_kode')";
}

function sql_divisi_kode3(){
	global $cdivisi_kode;
	return " and (divisi_kode='$cdivisi_kode')";
}

function arr_nggawe_halaman($max_record,$arr_baris,$jenis1,$jenis2,$jenis3,$jenis4,$jenis5="tanda_tangan"){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;

	$record=$max_record;
	$posisi_ideal=43;
	$baris_ideal=($record*ceil(count($arr_baris)/$record));
	$jml_hal=$baris_ideal/$record;
	$record=$posisi_ideal;
	$record_min_4=$record-4;
	$record_min_telu=$record-3;
	$record_min_2=$record-2;
	$record_min_1=$record-1;
	$record_min_3=$record-8;
	echo $arr_baris[0][isi];
	$pr_baris=1;
	for($h=0;$h<=$jml_hal;$h++){
		for($ib=1;$ib<=$posisi_ideal;$ib++){
			$print_n=true;	
			if(($ib % $record_min_2==0)or($ib % $record_min_1==0)or($ib % $record_min_3==1)or($ib % $record==0)or($ib % $record_min_telu==0)or($ib % $record_min_4==0)){
				if($ib % $record_min_4==0){
					$jml_chek=3;
					//echo " $ib -- $pr_baris --".$arr_baris[$pr_baris][jenis]."---- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
					if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4 and $arr_baris[$pr_baris-5][jenis]==$jenis1){
						for($jb=0;$jb<=$jml_chek+1;$jb++){
							//echo $arr_baris[0][isi];
							//echo $arr_baris[-1][isi];
							echo str_replace('jamal','jamal pinter kok',$arr_baris[-1][isi]);
							//$pr_baris--;
							$ib++;
						}
						$print_n=false;
					}
				}elseif($ib % $record_min_telu==0){
					$jml_chek=3;
					//echo " $ib -- $pr_baris --".$arr_baris[$pr_baris][jenis]."---- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
					if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4){
						for($jb=0;$jb<=$jml_chek+1;$jb++){
							//echo $arr_baris[0][isi];
							//echo $arr_baris[-1][isi];
							echo str_replace('jamal','jamal neng ndi',$arr_baris[-1][isi]);
							//$pr_baris--;
							$ib++;
						}
						$print_n=false;
					}
				}elseif(($ib % $record_min_2==0)){
					$jml_chek=3;
					if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis3 and $arr_baris[$pr_baris+$jml_chek+1][jenis]<>$jenis4){
						for($jb=0;$jb<=$jml_chek;$jb++){
							//echo $arr_baris[0][isi];
							//echo $arr_baris[-1][isi];
							echo str_replace('jamal','jamal ngganteng',$arr_baris[-1][isi]);
							//$pr_baris--;
							$ib++;
						}
						$print_n=false;
						//	}
						//}elseif(($ib % $record_min_2==0)){
						//	$jml_chek=3;
			}elseif($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4){
				for($jb=0;$jb<=$jml_chek;$jb++){
					//echo $arr_baris[0][isi];
					//echo $arr_baris[-1][isi];
					echo str_replace('jamal','jamal 1',$arr_baris[-1][isi]);
					//$pr_baris--;
					$ib++;
				}
				$print_n=false;
			}
			}elseif(($ib % $record_min_3)==1 and $ib!=1){
				$jml_chek=4;
				//echo "xxxxx $ib -- $pr_baris --".$arr_baris[$pr_baris][jenis]."---- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
				//echo "xxxxx $ib -- $pr_baris -- $record_min_3 ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis5){
					//echo "masukkkkkkkkkkkkkkk $jenis5";
					for($jb=1;$jb<=9;$jb++){
						//echo $arr_baris[0][isi];
						//echo $arr_baris[-1][isi];
						echo str_replace('jamal','jamal 2',$arr_baris[-1][isi]);
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}
			}elseif(($ib % $record_min_1==0)){
				$jml_chek=2;
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis3 and $arr_baris[$pr_baris-1][jenis]<>$jenis1){
					for($jb=0;$jb<=$jml_chek;$jb++){
						//echo $arr_baris[0][isi];
						echo str_replace('jamal','jamal 9',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}elseif($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis2 and $arr_baris[$pr_baris+$jml_chek+1][jenis]==$jenis4){
					for($jb=0;$jb<=$jml_chek;$jb++){
						//echo $arr_baris[0][isi];
						echo str_replace('jamal','jamal 3',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}
			}elseif(($ib % $record==0)){
				$jml_chek=1;
				//echo " test $pr_baris -- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis2){
					for($jb=0;$jb<=$jml_chek;$jb++){
						echo str_replace('jamal','jamal 4',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}elseif($arr_baris[$pr_baris][jenis]==$jenis1){
					for($jb=0;$jb<=$jml_chek;$jb++){
						echo str_replace('jamal','jamal 5',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
				}
			}

			if($print_n){
				//echo "$ib -- $pr_baris -- ".$arr_baris[$pr_baris][isi];
				//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
				echo $arr_baris[$pr_baris][isi];
				$pr_baris++;
			}
		}else{
			//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
			//echo "$pr_baris --". $arr_baris[$pr_baris][isi];
			echo $arr_baris[$pr_baris][isi];
			$pr_baris++;
		}
		//}
}
for($j=$ib;$j<=$max_record;$j++){
	if(($baris_ideal-$pr_baris)>5){
		echo str_replace('jamal',"jamal 6 $pr_baris -- $ib -- $baris_ideal",$arr_baris[-1][isi]);
		$ib++;
	}
}
if($pr_baris>=(count($arr_baris)-1)){
	//echo "testtt";
	break;	
}

}
return $arr_baris;
//echo $arr_baris[$pr_baris][isi];
}

function nggawe_hal_komparatif_konsolidasi($max_record,$arr_baris,$jenis1,$jenis2,$jenis3,$jenis4,$jenis5="tanda_tangan"){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;

	$record=$max_record;
	$posisi_ideal=43;
	$baris_ideal=($record*ceil(count($arr_baris)/$record));
	$jml_hal=$baris_ideal/$record;
	$record=$posisi_ideal;
	$record_min_4=$record-4;
	$record_min_telu=$record-3;
	$record_min_2=$record-2;
	$record_min_1=$record-1;
	$record_min_3=$record-8;
	echo $arr_baris[0][isi];
	$pr_baris=1;
	for($h=0;$h<=$jml_hal;$h++){
		for($ib=1;$ib<=$posisi_ideal;$ib++){
			$print_n=true;	
			if(($ib % $record_min_2==0)or($ib % $record_min_1==0)or($ib % $record_min_3==1)or($ib % $record==0)or($ib % $record_min_telu==0)or($ib % $record_min_4==0)){
				if($ib % $record_min_4==0){
					$jml_chek=3;
					//echo " $ib -- $pr_baris --".$arr_baris[$pr_baris][jenis]."---- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
					if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4 and $arr_baris[$pr_baris-5][jenis]==$jenis1){
						for($jb=0;$jb<=$jml_chek+1;$jb++){
							//echo $arr_baris[0][isi];
							//echo $arr_baris[-1][isi];
							echo str_replace('jamal','jamal pinter kok',$arr_baris[-1][isi]);
							//$pr_baris--;
							$ib++;
						}
						$print_n=false;
					}
				}elseif($ib % $record_min_telu==0){
					$jml_chek=3;
					//echo " $ib -- $pr_baris --".$arr_baris[$pr_baris][jenis]."---- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
					if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4){
						for($jb=0;$jb<=$jml_chek+1;$jb++){
							//echo $arr_baris[0][isi];
							//echo $arr_baris[-1][isi];
							echo str_replace('jamal','jamal neng ndi',$arr_baris[-1][isi]);
							//$pr_baris--;
							$ib++;
						}
						$print_n=false;
					}
				}elseif(($ib % $record_min_2==0)){
					$jml_chek=3;
					if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis3 and $arr_baris[$pr_baris+$jml_chek+1][jenis]<>$jenis4){
						for($jb=0;$jb<=$jml_chek;$jb++){
							//echo $arr_baris[0][isi];
							//echo $arr_baris[-1][isi];
							echo str_replace('jamal','jamal ngganteng',$arr_baris[-1][isi]);
							//$pr_baris--;
							$ib++;
						}
						$print_n=false;
						//	}
						//}elseif(($ib % $record_min_2==0)){
						//	$jml_chek=3;
			}elseif($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4){
				for($jb=0;$jb<=$jml_chek;$jb++){
					//echo $arr_baris[0][isi];
					//echo $arr_baris[-1][isi];
					echo str_replace('jamal','jamal 1',$arr_baris[-1][isi]);
					//$pr_baris--;
					$ib++;
				}
				$print_n=false;
			}
			}elseif(($ib % $record_min_3)==1 and $ib!=1){
				$jml_chek=4;
				//echo "xxxxx $ib -- $pr_baris --".$arr_baris[$pr_baris][jenis]."---- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
				//echo "xxxxx $ib -- $pr_baris -- $record_min_3 ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis5){
					//echo "masukkkkkkkkkkkkkkk $jenis5";
					for($jb=1;$jb<=9;$jb++){
						//echo $arr_baris[0][isi];
						//echo $arr_baris[-1][isi];
						echo str_replace('jamal','jamal 2',$arr_baris[-1][isi]);
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}
			}elseif(($ib % $record_min_1==0)){
				$jml_chek=2;
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis3 and $arr_baris[$pr_baris-1][jenis]<>$jenis1){
					for($jb=0;$jb<=$jml_chek;$jb++){
						//echo $arr_baris[0][isi];
						echo str_replace('jamal','jamal 9',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}elseif($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis2 and $arr_baris[$pr_baris+$jml_chek+1][jenis]==$jenis4){
					for($jb=0;$jb<=$jml_chek;$jb++){
						//echo $arr_baris[0][isi];
						echo str_replace('jamal','jamal 3',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}
			}elseif(($ib % $record==0)){
				$jml_chek=1;
				//echo " test $pr_baris -- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis2){
					for($jb=0;$jb<=$jml_chek;$jb++){
						echo str_replace('jamal','jamal 4',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}elseif($arr_baris[$pr_baris][jenis]==$jenis1){
					for($jb=0;$jb<=$jml_chek;$jb++){
						echo str_replace('jamal','jamal 5',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
				}
			}

			if($print_n){
				//echo "$ib -- $pr_baris -- ".$arr_baris[$pr_baris][isi];
				//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
				echo $arr_baris[$pr_baris][isi];
				$pr_baris++;
			}
		}else{
			//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
			//echo "$pr_baris --". $arr_baris[$pr_baris][isi];
			echo $arr_baris[$pr_baris][isi];
			$pr_baris++;
		}
		//}
}
for($j=$ib;$j<=$max_record;$j++){
	if(($baris_ideal-$pr_baris)>5){
		echo str_replace('jamal',"jamal 6 $pr_baris -- $ib -- $baris_ideal",$arr_baris[-1][isi]);
		$ib++;
	}
}
if($pr_baris>=(count($arr_baris)-1)){
	//echo "testtt";
	break;	
}

}
return $arr_baris;
//echo $arr_baris[$pr_baris][isi];
}

function arr_sum_rencana_biaya($tahun,$panjang=4){
	global $cdivisi_account,$cdivisi_kode;
	global $tanda_account,$tanda_laba;
	$sql="select substr(kode_perkiraan,1,$panjang) as kode_perkiraan,sum(bln1+bln2+bln3+bln4+bln5+bln6+bln7+bln8+bln9+bln10+bln11+bln12) as nilai,identitas from tbl_rencana_biaya_umum where tahun='$tahun' ".sql_divisi_kode3()." group by substr(kode_perkiraan,1,$panjang),identitas order by kode_perkiraan";
	//echo $sql."<br>";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$kode_perkiraan=$r[kode_perkiraan];
		$identitas=$r[identitas];
		$arr_temp[$identitas][$kode_perkiraan]=$r[nilai];
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;
}

function arr_summary_account2($bulan,$tahun,$panjang=5,$mu="",$nama_tabel="tbl_summary_account"){
	global $cdivisi_account;
	global $tanda_account,$cnospk;
	//global $tanda_account;
	//$tanda_laba=tanda_lb(5);
	$tanda_laba=tanda_laba(2);
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	//	$bulan=ltrim(str_replace("0","",$bulan));
	$bulan=$bulan=="01"?1:$bulan;
	$bulan=$bulan=="02"?2:$bulan;
	$bulan=$bulan=="03"?3:$bulan;
	$bulan=$bulan=="04"?4:$bulan;
	$bulan=$bulan=="05"?5:$bulan;
	$bulan=$bulan=="06"?6:$bulan;
	$bulan=$bulan=="07"?7:$bulan;
	$bulan=$bulan=="08"?8:$bulan;
	$bulan=$bulan=="09"?9:$bulan;
	for($i=$bulan;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini=="" or $str_debet_ini=="dt_0"){
		$str_debet_ini="0";
	}
	if($str_kredit_ini=="" or $str_kredit_ini=="kt_0"){
		$str_kredit_ini="0";
	}
	for($i=1;$i<=$bulan;$i++){
		$str_debet_sampai_ini.="dt_$i + ";
		$str_kredit_sampai_ini.="kt_$i + ";
	}
	$str_debet_sampai_ini=substr($str_debet_sampai_ini,0,strlen($str_debet_sampai_ini)-3);
	$str_kredit_sampai_ini=substr($str_kredit_sampai_ini,0,strlen($str_kredit_sampai_ini)-3);
	if($str_debet_sampai_ini=="" or $str_debet_sampai_ini=="dt_0"){
		$str_debet_sampai_ini="0";
	}
	if($str_kredit_sampai_ini=="" or $str_kredit_sampai_ini=="kt_0"){
		$str_kredit_sampai_ini="0";
	}
	$str_debet=$str_debet_lalu.$str_debet_sampai_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_sampai_ini;
	$tambah_kode_cc=$cnospk==""?"and kode_cc='0000000000'":"and kode_cc='$cnospk'";
	//$kondisi_mu=$mu==""?" and mu='RP'":" and mu<>'RP'";
	if($mu==""){
		$sql="select account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini,sum($str_debet_sampai_ini) as jml_debet_sampai_ini,sum($str_kredit_sampai_ini) as jml_kredit_sampai_ini,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,kode_proyek,kode_customer,kode_cc from $nama_tabel where tahun='$tahun' and account_kode<>'' and trim(mu)='RP' $tambah_kode_cc ".sql_divisi_kode2()." group by tahun,account_kode,kode_proyek,kode_customer,kode_cc";
	}else{
		$sql="select account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini,sum($str_debet_sampai_ini) as jml_debet_sampai_ini,sum($str_kredit_sampai_ini) as jml_kredit_sampai_ini,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,kode_proyek,kode_customer,mu,kode_cc from $nama_tabel where tahun='$tahun' and account_kode<>'' and trim(mu)<>'RP' $tambah_kode_cc ".sql_divisi_kode2()." group by tahun,account_kode,kode_proyek,kode_customer,mu,kode_cc";

	}
	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$sub2_acc=substr($r[account_kode],0,2);
		//$sub2_acc=$acc;
		$kode_proyek=$r[kode_proyek];
		$kode_customer=$r[kode_customer];
                $kode_cc=$r[kode_cc];
		$mu=$r[mu];
		$kode_awal=substr($acc,0,$panjang);
		/*		if($panjang>=3){
				$saldo_lalu=strpos($tanda_laba,$acc)>0?$r[jml_debet_lalu]-$r[jml_kredit_lalu]:$r[jml_kredit_lalu]-$r[jml_debet_lalu];
				$saldo_ini=strpos($tanda_laba,$acc)>0?$r[jml_debet_ini]-$r[jml_kredit_ini]:$r[jml_kredit_ini]-$r[jml_debet_ini];
				$saldo_sampai_ini=strpos($tanda_laba,$acc)>0?$r[jml_debet_sampai_ini]-$r[jml_kredit_sampai_ini]:$r[jml_kredit_sampai_ini]-$r[jml_debet_sampai_ini];
				$saldo=strpos($tanda_account,$kode_awal)>0?$r[jml_debet]-$r[jml_kredit]:$r[jml_kredit]-$r[jml_debet];
				$saldo=strpos($tanda_laba,$acc)>0?($r[jml_debet]-$r[jml_kredit]):($r[jml_kredit]-$r[jml_debet]);
				}else{
		 */			
		$saldo_lalu=ereg($tanda_laba,$sub2_acc)?$r[jml_debet_lalu]-$r[jml_kredit_lalu]:$r[jml_kredit_lalu]-$r[jml_debet_lalu];
		$saldo_ini=ereg($tanda_laba,$sub2_acc)?$r[jml_debet_ini]-$r[jml_kredit_ini]:$r[jml_kredit_ini]-$r[jml_debet_ini];
		//$saldo=strpos($tanda_account,$kode_awal)>0?$r[jml_debet]-$r[jml_kredit]:$r[jml_kredit]-$r[jml_debet];
		$saldo_sampai_ini=ereg($tanda_laba,$sub2_acc)?$r[jml_debet_sampai_ini]-$r[jml_kredit_sampai_ini]:$r[jml_kredit_sampai_ini]-$r[jml_debet_sampai_ini];
		$saldo=ereg($tanda_laba,$sub2_acc)?($r[jml_debet]-$r[jml_kredit]):($r[jml_kredit]-$r[jml_debet]);
		//		}
		/**/
		if($mu==""){
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo"]=$saldo==""?0:$saldo;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["dbt_sampai_ini"]=$r[jml_debet_sampai_ini]==""?0:$r[jml_debet_sampai_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kdt_sampai_ini"]=$r[jml_kredit_sampai_ini]==""?0:$r[jml_kredit_sampai_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo_sampai_ini"]=$saldo_sampai_ini==""?0:$saldo_sampai_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["account_kode"]=1;
		}else{
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo"]=$saldo==""?0:$saldo;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["dbt_sampai_ini"]=$r[jml_debet_sampai_ini]==""?0:$r[jml_debet_sampai_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kdt_sampai_ini"]=$r[jml_kredit_sampai_ini]==""?0:$r[jml_kredit_sampai_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo_sampai_ini"]=$saldo_sampai_ini==""?0:$saldo_sampai_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["account_kode"]=1;
		}
	}
	if($inp_par[jenis_laporan]=="non_audit" || $inp_par[jenis_laporan]==""){
		//$sql="select account_kode,sum(nilai) as nilai,tahun from tbl_debet_kredit where tahun='$tahun' and date_part('month',periode_posting)='$bulan' and account_kode<>'' $sql_divisi $sql_cnospk group by tahun,account_kode";
 		$sqla="select substr((case when substr(account_kode,1,1)<'3' then account_kode else '31511' end),1,$panjang) as account_kode,sum(case when jenis='1' then nilai else 0-nilai end) as nilai,tahun from tbl_debet_kredit where tahun='$tahun' and no_transaksi like '%AD%' and account_kode<>'' $sql_divisi $sql_cnospk group by tahun,substr((case when substr(account_kode,1,1)<'3' then account_kode else '31511' end),1,$panjang)";
		$resa=dbresult($sqla);
		while($ra=pg_fetch_array($resa)){
			$acca=$ra[account_kode];
			$sub_acca=substr($acca,0,$panjang);
			$sub2_acca=substr($ra[account_kode],0,2);
			$kode_proyek=$ra[kode_proyek];
			$kode_customer=$ra[kode_customer];
        	        $kode_cc=$ra[kode_cc];
			$kode_awal=substr($acc,0,$panjang);
			$mu=$ra[mu];

		$saldo_lalu=ereg($tanda_laba,$sub2_acc)?$ra[jml_debet_lalu]-$ra[jml_kredit_lalu]:$ra[jml_kredit_lalu]-$ra[jml_debet_lalu];
		$saldo_ini=ereg($tanda_laba,$sub2_acc)?$ra[jml_debet_ini]-$ra[jml_kredit_ini]:$ra[jml_kredit_ini]-$ra[jml_debet_ini];
		$saldo_sampai_ini=ereg($tanda_laba,$sub2_acc)?$ra[jml_debet_sampai_ini]-$ra[jml_kredit_sampai_ini]:$ra[jml_kredit_sampai_ini]-$ra[jml_debet_sampai_ini];
		$saldo=ereg($tanda_laba,$sub2_acc)?($ra[jml_debet]-$ra[jml_kredit]):($ra[jml_kredit]-$ra[jml_debet]);
		if($mu==""){
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo"]=$saldo==""?0:$saldo;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["debet"]=$ra[jml_debet]==""?0:$ra[jml_debet];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kredit"]=$ra[jml_kredit]==""?0:$ra[jml_kredit];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["debet_lalu"]=$ra[jml_debet_lalu]==""?0:$ra[jml_debet_lalu];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kredit_lalu"]=$ra[jml_kredit_lalu]==""?0:$ra[jml_kredit_lalu];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["debet_ini"]=$ra[jml_debet_ini]==""?0:$ra[jml_debet_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kredit_ini"]=$ra[jml_kredit_ini]==""?0:$ra[jml_kredit_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["dbt_sampai_ini"]=$ra[jml_debet_sampai_ini]==""?0:$ra[jml_debet_sampai_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["kdt_sampai_ini"]=$ra[jml_kredit_sampai_ini]==""?0:$ra[jml_kredit_sampai_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["saldo_sampai_ini"]=$saldo_sampai_ini==""?0:$saldo_sampai_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$kode_cc]["account_kode"]=1;
		}else{
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo"]=$saldo==""?0:$saldo;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["debet"]=$ra[jml_debet]==""?0:$ra[jml_debet];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kredit"]=$ra[jml_kredit]==""?0:$ra[jml_kredit];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["debet_lalu"]=$ra[jml_debet_lalu]==""?0:$ra[jml_debet_lalu];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kredit_lalu"]=$ra[jml_kredit_lalu]==""?0:$ra[jml_kredit_lalu];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["debet_ini"]=$ra[jml_debet_ini]==""?0:$ra[jml_debet_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kredit_ini"]=$ra[jml_kredit_ini]==""?0:$ra[jml_kredit_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["dbt_sampai_ini"]=$ra[jml_debet_sampai_ini]==""?0:$ra[jml_debet_sampai_ini];
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["kdt_sampai_ini"]=$ra[jml_kredit_sampai_ini]==""?0:$ra[jml_kredit_sampai_ini];;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["saldo_sampai_ini"]=$saldo_sampai_ini==""?0:$saldo_sampai_ini;
			$arr_temp[$kode_awal][$kode_proyek][$kode_customer][$mu][$kode_cc]["account_kode"]=1;
		}
			$arr_temp[$sub_acca]["nilai_audit"]+=strpos($tanda_lb,$sub2_acca)>0?$ra[nilai]:0-$ra[nilai];
			$arr_temp[$sub_acca]["sd_ini"]=$arr_temp[$sub_acca]["sd_ini"]-$arr_temp[$sub_acca]["nilai_audit"];
			$arr_temp[$sub_acca]["sd_lalu"]=$arr_temp[$sub_acca]["sd_lalu"]-$arr_temp[$sub_acca]["nilai_audit"];
		}	
	}
	return $arr_temp;
}

function arr_buku_tambahan($bulan,$tahun,$panjang=5){
	global $cdivisi_account;
	global $tanda_account,$tanda_laba;
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	$bulan=ltrim(str_replace("0","",$bulan));	
	for($i=$bulan;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini=="" or $str_debet_ini=="dt_"){
		$str_debet_ini="0";
	}
	if($str_kredit_ini=="" or $str_kredit_ini=="kt_"){
		$str_kredit_ini="0";
	}
	for($i=1;$i<=$bulan;$i++){
		$str_debet_sampai_ini.="dt_$i + ";
		$str_kredit_sampai_ini.="kt_$i + ";
	}
	$str_debet_sampai_ini=substr($str_debet_sampai_ini,0,strlen($str_debet_sampai_ini)-3);
	$str_kredit_sampai_ini=substr($str_kredit_sampai_ini,0,strlen($str_kredit_sampai_ini)-3);
	if($str_debet_sampai_ini=="" or $str_debet_sampai_ini=="dt_"){
		$str_debet_sampai_ini="0";
	}
	if($str_kredit_sampai_ini=="" or $str_kredit_sampai_ini=="kt_"){
		$str_kredit_sampai_ini="0";
	}
	$str_debet=$str_debet_lalu.$str_debet_sampai_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_sampai_ini;

	//$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";

	//$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini,kode_proyek from tbl_summary_account where tahun='$tahun' ".sql_divisi_kode2()." group by tahun,substr(account_kode,1,$panjang),kode_proyek";
	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini,sum($str_debet_sampai_ini) as jml_debet_sampai_ini,sum($str_kredit_sampai_ini) as jml_kredit_sampai_ini,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,kode_proyek,kode_customer from tbl_summary_account where tahun='$tahun' ".sql_divisi_kode2()." group by tahun,substr(account_kode,1,$panjang),kode_proyek,kode_customer order by account_kode,kode_proyek,kode_customer";
	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$kode_proyek=$r[kode_proyek];
		$kode_customer=$r[kode_customer];
		$kode_awal=substr($acc,0,$panjang);
		if($panjang>=3){
			$saldo_lalu=strpos($tanda_laba,$kode_awal)>0?$r[jml_debet_lalu]-$r[jml_kredit_lalu]:$r[jml_kredit_lalu]-$r[jml_debet_lalu];
			$saldo_ini=strpos($tanda_laba,$kode_awal)>0?$r[jml_debet_ini]-$r[jml_kredit_ini]:$r[jml_kredit_ini]-$r[jml_debet_ini];
			//$saldo=strpos($tanda_account,$kode_awal)>0?$r[jml_debet]-$r[jml_kredit]:$r[jml_kredit]-$r[jml_debet];
			$saldo=strpos($tanda_laba,$kode_awal)>0?($r[jml_debet]-$r[jml_kredit]):($r[jml_kredit]-$r[jml_debet]);
			$saldo_sampai_ini=strpos($tanda_laba,$kode_awal)>0?$r[jml_debet_sampai_ini]-$r[jml_kredit_sampai_ini]:$r[jml_kredit_sampai_ini]-$r[jml_debet_sampai_ini];
		}else{
			$saldo_lalu=strpos($kode_awal,$tanda_laba)>0?$r[jml_debet_lalu]-$r[jml_kredit_lalu]:$r[jml_kredit_lalu]-$r[jml_debet_lalu];
			$saldo_ini=strpos($kode_awal,$tanda_laba)>0?$r[jml_debet_ini]-$r[jml_kredit_ini]:$r[jml_kredit_ini]-$r[jml_debet_ini];
			//$saldo=strpos($tanda_account,$kode_awal)>0?$r[jml_debet]-$r[jml_kredit]:$r[jml_kredit]-$r[jml_debet];
			$saldo=strpos($kode_awal,$tanda_laba)>0?($r[jml_debet]-$r[jml_kredit]):($r[jml_kredit]-$r[jml_debet]);
			$saldo_sampai_ini=strpos($kode_awal,$tanda_laba)>0?$r[jml_debet_sampai_ini]-$r[jml_kredit_sampai_ini]:$r[jml_kredit_sampai_ini]-$r[jml_debet_sampai_ini];
		}
		$arr_temp[$acc][$kode_proyek][$kode_customer]["saldo"]=$saldo==""?0:$saldo;
		$arr_temp[$acc][$kode_proyek][$kode_customer]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp[$acc][$kode_proyek][$kode_customer]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp[$acc][$kode_proyek][$kode_customer]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp[$acc][$kode_proyek][$kode_customer]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp[$acc][$kode_proyek][$kode_customer]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp[$acc][$kode_proyek][$kode_customer]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp[$acc][$kode_proyek][$kode_customer]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp[$acc][$kode_proyek][$kode_customer]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
		$arr_temp[$acc][$kode_proyek][$kode_customer]["dbt_sampai_ini"]=$r[jml_debet_sampai_ini]==""?0:$r[jml_debet_sampai_ini];
		$arr_temp[$acc][$kode_proyek][$kode_customer]["kdt_sampai_ini"]=$r[jml_kredit_sampai_ini]==""?0:$r[jml_kredit_sampai_ini];;
		$arr_temp[$acc][$kode_proyek][$kode_customer]["saldo_sampai_ini"]=$saldo_sampai_ini==""?0:$saldo_sampai_ini;
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;
}

function arr_summary_account3($bulan,$tahun,$panjang=3,$divisi=""){
	global $cdivisi_account,$cnospk;
	global $tanda_account;
	if($divisi==""){
		$sql_divisi=sql_divisi_kode2();
	}else{
		$sql_divisi=" and (data_divisi_kode='$divisi')";
	}
	//$tanda_lb=tanda_lb();
	if($cnospk=="0000000000" || $cnospk==""){
		$sql_cnospk="";
	}else{
		$sql_cnospk=" and kode_proyek='$cnospk'";
	}
	$tanda_lb=tanda_laba();
	$bulan=$bulan=="01"?1:$bulan;
	$bulan=$bulan=="02"?2:$bulan;
	$bulan=$bulan=="03"?3:$bulan;
	$bulan=$bulan=="04"?4:$bulan;
	$bulan=$bulan=="05"?5:$bulan;
	$bulan=$bulan=="06"?6:$bulan;
	$bulan=$bulan=="07"?7:$bulan;
	$bulan=$bulan=="08"?8:$bulan;
	$bulan=$bulan=="09"?9:$bulan;
	//$bulan=ltrim(str_replace("0","",$bulan));	
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	for($i=$bulan;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini=="" or $str_debet_ini=="dt_0"){
		$str_debet_ini="0";
	}
	if($str_kredit_ini=="" or $str_kredit_ini=="kt_0"){
		$str_kredit_ini="0";
	}
	for($i=1;$i<=$bulan;$i++){
		$str_debet_sampai_ini.="dt_$i + ";
		$str_kredit_sampai_ini.="kt_$i + ";
	}
	$str_debet_sampai_ini=substr($str_debet_sampai_ini,0,strlen($str_debet_sampai_ini)-3);
	$str_kredit_sampai_ini=substr($str_kredit_sampai_ini,0,strlen($str_kredit_sampai_ini)-3);
	if($str_debet_sampai_ini=="" or $str_debet_sampai_ini=="dt_0"){
		$str_debet_sampai_ini="0";
	}
	if($str_kredit_sampai_ini=="" or $str_kredit_sampai_ini=="kt_0"){
		$str_kredit_sampai_ini="0";
	}
	$str_debet=$str_debet_lalu.$str_debet_sampai_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_sampai_ini;

	//$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";

#$sql="select account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini,sum($str_debet_sampai_ini) as jml_debet_sampai_ini,sum($str_kredit_sampai_ini) as jml_kredit_sampai_ini,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from tbl_summary_account where tahun='$tahun' and account_kode<>'' ".sql_divisi_kode2()." group by tahun,account_kode";
	$sql="select account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini,sum($str_debet_sampai_ini) as jml_debet_sampai_ini,sum($str_kredit_sampai_ini) as jml_kredit_sampai_ini,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from tbl_summary_account where tahun='$tahun' and account_kode<>'' and mu='RP' $sql_divisi $sql_cnospk group by tahun,account_kode";
	$res=dbresult($sql);
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$kode_awal=substr($acc,0,$panjang);
		$sub2_acc=substr($r[account_kode],0,2);
		//echo $kode_awal."A<br>";
		//$saldo_lalu=ereg($tanda_lb,$acc)?$r[jml_debet_lalu]-$r[jml_kredit_lalu]:$r[jml_kredit_lalu]-$r[jml_debet_lalu];
		//$saldo_ini=ereg($tanda_lb,$acc)?$r[jml_debet_ini]-$r[jml_kredit_ini]:$r[jml_kredit_ini]-$r[jml_debet_ini];
		//$saldo_sampai_ini=ereg($tanda_lb,$acc)?$r[jml_debet_sampai_ini]-$r[jml_kredit_sampai_ini]:$r[jml_kredit_sampai_ini]-$r[jml_debet_sampai_ini];
		//$saldo=ereg($tanda_lb,$acc)?($r[jml_debet]-$r[jml_kredit]):($r[jml_kredit]-$r[jml_debet]);
		$saldo_lalu=ereg($tanda_lb,$sub2_acc)?$r[jml_debet_lalu]-$r[jml_kredit_lalu]:$r[jml_kredit_lalu]-$r[jml_debet_lalu];
		$saldo_ini=ereg($tanda_lb,$sub2_acc)?$r[jml_debet_ini]-$r[jml_kredit_ini]:$r[jml_kredit_ini]-$r[jml_debet_ini];
		$saldo_sampai_ini=ereg($tanda_lb,$sub2_acc)?$r[jml_debet_sampai_ini]-$r[jml_kredit_sampai_ini]:$r[jml_kredit_sampai_ini]-$r[jml_debet_sampai_ini];
		//echo "$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini] <br>";

		$saldo=ereg($tanda_lb,$sub2_acc)?($r[jml_debet]-$r[jml_kredit]):($r[jml_kredit]-$r[jml_debet]);
		$arr_temp[$kode_awal]["saldo"]+=$saldo==""?0:$saldo;
		$arr_temp[$kode_awal]["debet"]+=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp[$kode_awal]["kredit"]+=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp[$kode_awal]["debet_lalu"]+=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp[$kode_awal]["kredit_lalu"]+=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp[$kode_awal]["saldo_lalu"]+=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp[$kode_awal]["debet_ini"]+=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp[$kode_awal]["kredit_ini"]+=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp[$kode_awal]["saldo_ini"]+=$saldo_ini==""?0:$saldo_ini;
		$arr_temp[$kode_awal]["debet_sampai_ini"]+=$r[jml_debet_sampai_ini]==""?0:$r[jml_debet_sampai_ini];
		$arr_temp[$kode_awal]["kredit_sampai_ini"]+=$r[jml_kredit_sampai_ini]==""?0:$r[jml_kredit_sampai_ini];;
		$arr_temp[$kode_awal]["saldo_sampai_ini"]+=$saldo_sampai_ini==""?0:$saldo_sampai_ini;
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;

}

function lihat_jumlah_bap($cdivisi_kode,$no_invoice_c,$jenis_invoice,$dinv_bap){
	global $inp,$inp_par,$cdivisi_kode;
	$sql="select sum(dinv_jumlah) as jumlah from tbl_detail_invoice where divisi_kode='$cdivisi_kode' and no_invoice_c='$no_invoice_c' and jenis_invoice='$jenis_invoice' and dinv_berita_acara='$dinv_bap'";
	$result=dbresult($sql);
	$r=pg_fetch_array($result);
	$jumlah=$r[jumlah]==""?0:$r[jumlah];
	return $jumlah;
}

function lihat_jumlah_beda_bap($cdivisi_kode,$no_invoice_c,$jenis_invoice,$dinv_bap){
	global $inp,$inp_par,$cdivisi_kode;
	$sql="select sum(dinv_jumlah) as jumlah from tbl_detail_invoice where divisi_kode='$cdivisi_kode' and no_invoice_c='$no_invoice_c' and jenis_invoice='$jenis_invoice'";
	$result=dbresult($sql);
	$r=pg_fetch_array($result);
	$jumlah=$r[jumlah]==""?0:$r[jumlah];
	return $jumlah;
}

function lihat_jml_not_bap($cdivisi_kode,$no_invoice_c,$jenis_invoice,$dinv_bap){
	global $inp,$inp_par,$cdivisi_kode;
	$sql="select sum(dinv_jumlah) as jumlah from tbl_detail_invoice where divisi_kode='$cdivisi_kode' and no_invoice_c='$no_invoice_c' and jenis_invoice='$jenis_invoice' and dinv_berita_acara<>'$dinv_bap'";
	$result=dbresult($sql);
	$r=pg_fetch_array($result);
	$jumlah=$r[jumlah]==""?0:$r[jumlah];
	return $jumlah;
}

function lihat_jumlah($cdivisi_kode,$no_invoice_c,$jenis_invoice){
	global $inp,$inp_par,$cdivisi_kode;
	$sql="select sum(dinv_jumlah) as jumlah from tbl_detail_invoice where divisi_kode='$cdivisi_kode' and no_invoice_c='$no_invoice_c' and jenis_invoice='$jenis_invoice'";
	$result=dbresult($sql);
	$r=pg_fetch_array($result);
	$jumlah=$r[jumlah]==""?0:$r[jumlah];
	return $jumlah;
}

function arr_summary_account5($bulan,$tahun,$bulan2,$tahun2,$panjang=3){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$bulan=$bulan=="01"?1:$bulan;
	$bulan=$bulan=="02"?2:$bulan;
	$bulan=$bulan=="03"?3:$bulan;
	$bulan=$bulan=="04"?4:$bulan;
	$bulan=$bulan=="05"?5:$bulan;
	$bulan=$bulan=="06"?6:$bulan;
	$bulan=$bulan=="07"?7:$bulan;
	$bulan=$bulan=="08"?8:$bulan;
	$bulan=$bulan=="09"?9:$bulan;
	if($bulan=="1"){
		$str_debet_lalu="dsll + ";
		$str_kredit_lalu="ksll + ";
	}
	//	$str_debet_lalu=substr($str_debet_lalu,0,strlen($str_debet_lalu)-4);
	//	$str_kredit_lalu=substr($str_kredit_lalu,0,strlen($str_kredit_lalu)-4);
	for($i=$bulan;$i<=$bulan2;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	$bulan=trim(str_replace("0","",$bulan));	
	if($bulan=="1"){
		$str_debet=$str_debet_lalu.$str_debet_ini;
		$str_kredit=$str_kredit_lalu.$str_kredit_ini;
	}else{
		$str_debet=$str_debet_ini;
		$str_kredit=$str_kredit_ini;
	}
	//	$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";
	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' ".sql_divisi_kode()." group by tahun,substr(account_kode,1,$panjang)";
	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$kode_awal=substr($acc,0,$panjang);
		//echo $kode_awal."A<br>";
		$saldo_lalu=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_lalu]-$r[jml_debet_lalu]:$r[jml_debet_lalu]-$r[jml_kredit_lalu];
		$saldo_ini=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_ini]-$r[jml_debet_ini]:$r[jml_debet_ini]-$r[jml_kredit_ini];
		//echo "$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini] <br>";

		$saldo=strpos($tanda_account,$kode_awal)>0?($r[jml_kredit]-$r[jml_debet]):($r[jml_debet]-$r[jml_kredit]);
		$arr_temp[$acc]["saldo"]=$saldo==""?0:$saldo;
		$arr_temp[$acc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp[$acc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp[$acc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp[$acc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp[$acc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp[$acc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp[$acc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp[$acc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;

}

function arr_summary_account15($bulan,$tahun,$bulan2,$tahun2,$panjang=1){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$bulan=$bulan=="01"?1:$bulan;
	$bulan=$bulan=="02"?2:$bulan;
	$bulan=$bulan=="03"?3:$bulan;
	$bulan=$bulan=="04"?4:$bulan;
	$bulan=$bulan=="05"?5:$bulan;
	$bulan=$bulan=="06"?6:$bulan;
	$bulan=$bulan=="07"?7:$bulan;
	$bulan=$bulan=="08"?8:$bulan;
	$bulan=$bulan=="09"?9:$bulan;
	if($bulan=="1"){
		$str_debet_lalu="dsll + ";
		$str_kredit_lalu="ksll + ";
	}
	//	$str_debet_lalu=substr($str_debet_lalu,0,strlen($str_debet_lalu)-4);
	//	$str_kredit_lalu=substr($str_kredit_lalu,0,strlen($str_kredit_lalu)-4);
	for($i=$bulan;$i<=$bulan2;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	$bulan=trim(str_replace("0","",$bulan));	
	if($bulan=="1"){
		$str_debet=$str_debet_lalu.$str_debet_ini;
		$str_kredit=$str_kredit_lalu.$str_kredit_ini;
	}else{
		$str_debet=$str_debet_ini;
		$str_kredit=$str_kredit_ini;
	}
	//	$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";
	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' ".sql_divisi_kode()." group by tahun,substr(account_kode,1,$panjang)";
	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$kode_awal=substr($acc,0,1);
		//echo $kode_awal."A<br>";
		$saldo_lalu=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_lalu]-$r[jml_debet_lalu]:$r[jml_debet_lalu]-$r[jml_kredit_lalu];
		$saldo_ini=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_ini]-$r[jml_debet_ini]:$r[jml_debet_ini]-$r[jml_kredit_ini];
		//echo "$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini] <br>";

		$saldo=strpos($tanda_account,$kode_awal)>0?($r[jml_kredit]-$r[jml_debet]):($r[jml_debet]-$r[jml_kredit]);
		$arr_temp[$acc]["saldo"]=$saldo==""?0:$saldo;
		$arr_temp[$acc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp[$acc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp[$acc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp[$acc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp[$acc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp[$acc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp[$acc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp[$acc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;

	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;

}

function cariDivisi($divisi_kode){
	$sql = "select divisi_name from tbl_dm_divisi where divisi_kode = '$divisi_kode'";
	$res = dbresult($sql);
	$r=pg_fetch_array($res);
	return $r[divisi_name];
}


function baca_rumus($rumus,$arr_nilai){
	if(is_array($arr_nilai)){
		while(list($key,$nilai)=each($arr_nilai)){
			$x=format_mede($nilai,2);
			$arr_nilai[$key]=str_replace(",","",$x);
		}
	}
	/*	echo $rumus." yyyy <br>";
		echo "<pre>";
		print_r($arr_nilai);
		echo "</pre>";
	 */	
	eval ("\$rumus= \"$rumus\";"); 
	return  kalkulator("$rumus");
}
function kalkulator($sum){
	$sum=str_replace("--","+",$sum);
	//			echo $sum." xxxx<br>";
	$check = strtr($sum, "0123456789+-*/().", "                 ");
	//		echo $check."<br>";
	if ( trim($check) == "" ) {
		$to_eval="\$evalled=" . $sum . ";";
		//echo $to_eval." Makan Ati<br>";
		eval($to_eval);
	}else{
		//echo "$sum <br>";
		$evalled = "Perhitungan Salah";
	}
	return $evalled;
}

function nggawe_halaman_laba_rugi($max_record,$arr_baris,$jenis1,$jenis2,$jenis3,$jenis5="tanda_tangan"){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;

	$record=$max_record;
	$posisi_ideal=43;
	$baris_ideal=($record*ceil(count($arr_baris)/$record));
	$jml_hal=$baris_ideal/$record;
	$record=$posisi_ideal;
	$record_min_4=$record-4;
	$record_min_telu=$record-3;
	$record_min_2=$record-2;
	$record_min_1=$record-1;
	$record_min_3=$record-6;
#	echo $arr_baris[0][isi];
	$pr_baris=1;
	for($h=0;$h<=$jml_hal;$h++){
		for($ib=1;$ib<=$record+1;$ib++){
			$print_n=true;
			if(($ib % ($posisi_ideal+1) ==0)){
				if($pr_baris<=(count($arr_baris)-1)){
					for($jb=1;$jb<=1;$jb++){
						echo str_replace('jamal','jamal bt',$arr_baris[-1][isi]);
						$ib++;
					}
				}
			}else{
				if(($ib % $record_min_2==0)or($ib % $record_min_1==0)or($ib % $record_min_3==0)or($ib % $record==0)or($ib % $record_min_telu==0)){
					if(($ib % $record_min_2==0)){
						$jml_chek=3;
						if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis3 and $arr_baris[$pr_baris+$jml_chek+1][jenis]==$jenis5){
							for($jb=0;$jb<=$jml_chek;$jb++){
								echo str_replace('jamal','jamal ngganteng',$arr_baris[-1][isi]);
								$ib++;
							}
							$print_n=false;
						}
					}elseif(($ib % $record_min_3)==0 and $ib!=1){
						$jml_chek=4;
						if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis5){
							for($jb=1;$jb<=8;$jb++){
								echo str_replace('jamal','jamal 2',$arr_baris[-1][isi]);
								$ib++;
							}
							$print_n=false;
						}
					}elseif(($ib % $record_min_1==0)){
						$jml_chek=2;
						if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis3 and $arr_baris[$pr_baris+1][jenis]==$jenis2){
							for($jb=0;$jb<=$jml_chek;$jb++){
								echo str_replace('jamal','jamal 9',$arr_baris[-1][isi]);
								$ib++;
							}
							$print_n=false;
						}elseif($arr_baris[$pr_baris][jenis]==$jenis1){
							for($jb=0;$jb<=$jml_chek;$jb++){
								echo str_replace('jamal','jamal piro',$arr_baris[-1][isi]);
								//$pr_baris--;
								$ib++;
							}
							$print_n=false;
						}
					}elseif(($ib % $record==0)){
						$jml_chek=1;
						if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis2){
							for($jb=0;$jb<=$jml_chek+1;$jb++){
								echo str_replace('jamal','jamal 4',$arr_baris[-1][isi]);
								$ib++;
							}
							$print_n=false;
						}elseif($arr_baris[$pr_baris][jenis]==$jenis1){
							//echo " test $pr_baris -- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
							for($jb=0;$jb<=$jml_chek;$jb++){
								echo str_replace('jamal','jamal 5',$arr_baris[-1][isi]);
								$ib++;
							}
						}
					}
					if($print_n){
						echo $arr_baris[$pr_baris][isi];
						$pr_baris++;
					}
				}else{
					echo $arr_baris[$pr_baris][isi];
					$pr_baris++;
				}
			}
		}
		if($pr_baris>=(count($arr_baris)-1)){
			//echo "testtt";
			break;	
		}

	}
	return $arr_baris;
	//echo $arr_baris[$pr_baris][isi];
}

function nggawe_halaman_neraca_lajur($max_record,$arr_baris,$jenis1,$jenis2,$jenis3,$jenis4,$jenis5="tanda_tangan"){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$record=$max_record;
	$posisi_ideal=27;
	$baris_ideal=($record*ceil(count($arr_baris)/$record));
	$jml_hal=$baris_ideal/$record;
	$record=$posisi_ideal;
	$record_min_2=$record-2;
	$record_min_1=$record-1;
	$record_min_3=$record-5;
	echo $arr_baris[0][isi];
	$pr_baris=1;
	//echo "$jml_hal";
	for($h=0;$h<=$jml_hal;$h++){
		//echo "$h <br>";
		for($ib=1;$ib<=$record+1;$ib++){
			$print_n=true;
			//if(($ib % ($posisi_ideal+1) ==0) and ($baris_ideal-$pr_baris)>18){
			if(($ib % ($posisi_ideal+1) ==0)){
				//echo " Opo Tho $pr_baris -- $baris_ideal --$ib "."<br>";
				if($pr_baris<=(count($arr_baris)-1)){
					for($jb=1;$jb<=2;$jb++){
						echo str_replace('jamal','jamal bt',$arr_baris[-1][isi]);
						$ib++;
					}
				}
			}else{
				//if(($ib % 43==0)or($ib % 44==0)){
				//echo str_replace('xxx','yyy',$arr_baris[0][isi]);
				//}else{
				if(($ib % $record_min_2==0) or ($ib % $record_min_1==1) or ($ib % $record==0) or ($ib % $record_min_3==0)){
					if(($ib % $record_min_2==0)){
						$jml_chek=3;
						//echo " ehmehemset $pr_baris -- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
						if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4 and $jenis4<>""){
							for($jb=1;$jb<=$jml_chek+2;$jb++){
								//echo $arr_baris[0][isi];
								//echo $arr_baris[-1][isi];
								echo str_replace('jamal','jamal 1',$arr_baris[-1][isi]);
								//$pr_baris--;
								$ib++;
							}
							$print_n=false;
						}

					}elseif(($ib % $record_min_1==0)){
						$jml_chek=2;
						//echo " testing $pr_baris -- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
						if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4){
							for($jb=1;$jb<=$jml_chek;$jb++){
								//echo $arr_baris[0][isi];
								echo str_replace('jamal','jamal 3',$arr_baris[-1][isi]);
								//echo $arr_baris[-1][isi];
								//$pr_baris--;
								$ib++;
							}
							$print_n=false;
						}
					}elseif(($ib % $record)==0){
						$jml_chek=1;
						//echo " test $pr_baris -- $ib ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
						if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis2 and $arr_baris[$pr_baris+$jml_chek+1][jenis]<>$jenis2){
							for($jb=1;$jb<=3;$jb++){
								echo str_replace('jamal',"$ib $jenis2",$arr_baris[-1][isi]);
								//echo $arr_baris[-1][isi];
								//$pr_baris--;
								$ib++;
							}
							$print_n=false;
						}

					}
					if($print_n){
						//echo "$ib -- $pr_baris -- ".$arr_baris[$pr_baris][isi];
						//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
						echo $arr_baris[$pr_baris][isi];
						$pr_baris++;
					}
				}else{
					//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
					echo $arr_baris[$pr_baris][isi];
					$pr_baris++;
				}			//}
			}			//}
		}
		//echo "testtt $pr_baris ".count($arr_baris)."<br>";
		if($pr_baris>=(count($arr_baris)-1)){
			//echo "testtt";
			exit;
		}
	}
	return $arr_baris;
	//echo $arr_baris[$pr_baris][isi];
}
function arr_summary_account10($bulan,$tahun,$bulan2,$tahun2,$panjang=12){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$bulan=$bulan=="01"?1:$bulan;
	$bulan=$bulan=="02"?2:$bulan;
	$bulan=$bulan=="03"?3:$bulan;
	$bulan=$bulan=="04"?4:$bulan;
	$bulan=$bulan=="05"?5:$bulan;
	$bulan=$bulan=="06"?6:$bulan;
	$bulan=$bulan=="07"?7:$bulan;
	$bulan=$bulan=="08"?8:$bulan;
	$bulan=$bulan=="09"?9:$bulan;
	if($bulan=="1"){
		$str_debet_lalu="dsll + ";
		$str_kredit_lalu="ksll + ";
	}
	//	$str_debet_lalu=substr($str_debet_lalu,0,strlen($str_debet_lalu)-4);
	//	$str_kredit_lalu=substr($str_kredit_lalu,0,strlen($str_kredit_lalu)-4);
	for($i=$bulan;$i<=$bulan2;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	$bulan=trim(str_replace("0","",$bulan));	
	if($bulan=="1"){
		$str_debet=$str_debet_lalu.$str_debet_ini;
		$str_kredit=$str_kredit_lalu.$str_kredit_ini;
	}else{
		$str_debet=$str_debet_ini;
		$str_kredit=$str_kredit_ini;
	}
	//	$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";
	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' ".sql_account($cdivisi_account)." group by tahun,substr(account_kode,1,$panjang)";
	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		$kode_awal=substr($acc,0,3);
		$saldo_lalu=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_lalu]-$r[jml_debet_lalu]:$r[jml_debet_lalu]-$r[jml_kredit_lalu];
		$saldo_ini=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_ini]-$r[jml_debet_ini]:$r[jml_debet_ini]-$r[jml_kredit_ini];
		//echo "$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini] <br>";

		$saldo=strpos($tanda_account,$kode_awal)>0?($r[jml_kredit]-$r[jml_debet]):($r[jml_debet]-$r[jml_kredit]);
		$arr_temp[$acc]["saldo"]=$saldo==""?0:$saldo;
		$arr_temp[$acc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp[$acc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp[$acc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp[$acc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp[$acc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp[$acc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp[$acc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp[$acc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;

	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;

}

function sql_account2($kode_account){
	$arr_x=split(",",$kode_account);
	while(list($kunci,$nilai)=each($arr_x)){
		$txt.=" account_kode like('%.$nilai.%') or ";
	}
	if($txt<>""){
		$txt=substr($txt,0,strlen($txt)-4);
		$txt=" ($txt)";
	}
	return $txt;
}

function format_dk($angka,$digit=0,$xx="",$yy=""){
	if($angka <=0){
		$str_temp= number_format((0-$angka),$digit)." K";
	}else{
		$str_temp= number_format($angka,$digit). " D";
	}
	return $str_temp;
}
function format_ak($angka,$digit=0){
	if($angka <0){
		$str_temp= "<font face=arial size=1 color=red><b>(".number_format((0-$angka),$digit).")<b></font>";
	}else{
		$str_temp= number_format($angka,$digit);
	}
	return $str_temp;
}
function format_ak_print($angka,$digit=0){
	if($angka <0){
		$str_temp= "(".number_format((0-$angka),$digit).")</font>";
	}else{
		$str_temp= number_format($angka,$digit);
	}
	return $str_temp;
}

function arr_nama_ikhtisar($tahun){
	global $cdivisi_account,$cdivisi_kode;
	global $tanda_account,$tanda_lb;
	$arr_kdaccount=split(",",$cdivisi_account);
	while(list($key,$nilai)=each($arr_kdaccount)){	
		$sql="select account_kode,account_nama from tbl_account where tahun='$tahun' and account_kode like('4%0.%')".sql_account($nilai)." and account_kode like('%0.%%%.%') group by account_kode,account_nama order by account_kode";
		$res=dbresult($sql);//echo $sql;
		//echo $sql."<br>";
		while($r=pg_fetch_array($res)){
			$acc=substr($r[account_kode],0,11);
			$kode1=substr($r[account_kode],0,2);
			$kode2=substr($r[account_kode],4,7);
			$arr_temp[$kode1][$kode2]["keterangan"]=$r[account_nama];
			$arr_temp[$kode1][$kode2]["account_kode"]=$acc;
		}
	}
	/*
	   echo "<pre>";
	   print_r($arr_temp);
	   echo "</pre>";
	 */
	return $arr_temp;
}



function arr_summ_per_akt_tetap1($bulan,$tahun,$panjang=3){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	for($i=1;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	$bulan=trim(str_replace("0","",$bulan));	
	$str_debet=$str_debet_lalu.$str_debet_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_ini;
	//	$sql="select  sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit from qry_summary_account where tahun='$tahun' and account_kode ='$account_kode%'";
	$sql="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' ".sql_account($cdivisi_account)." and account_kode like '18%.%' and account_kode like '%.%%%0' group by substr(account_kode,1,$panjang) order by substr(account_kode,1,$panjang)";
	$res=dbresult($sql);//echo $sql;
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$acc=$r[account_kode];
		//		$acc=substr($r[account_kode],0,3);
		$kode_awal=$acc;
		$saldo_lalu=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_lalu]-$r[jml_debet_lalu]:$r[jml_debet_lalu]-$r[jml_kredit_lalu];
		$saldo_ini=strpos($tanda_account,$kode_awal)>0?$r[jml_kredit_ini]-$r[jml_debet_ini]:$r[jml_debet_ini]-$r[jml_kredit_ini];
		//echo "$saldo_ini=$r[jml_debet_ini]-$r[jml_kredit_ini] <br>";

		$saldo1=strpos($tanda_account,$kode_awal)>0?($r[jml_kredit]-$r[jml_debet]):($r[jml_debet]-$r[jml_kredit]);
		$arr_temp1[$acc]["saldo"]=$saldo1==""?0:$saldo1;
		$arr_temp1[$acc]["debet"]=$r[jml_debet]==""?0:$r[jml_debet];
		$arr_temp1[$acc]["kredit"]=$r[jml_kredit]==""?0:$r[jml_kredit];;
		$arr_temp1[$acc]["debet_lalu"]=$r[jml_debet_lalu]==""?0:$r[jml_debet_lalu];
		$arr_temp1[$acc]["kredit_lalu"]=$r[jml_kredit_lalu]==""?0:$r[jml_kredit_lalu];;
		$arr_temp1[$acc]["saldo_lalu"]=$saldo_lalu==""?0:$saldo_lalu;
		$arr_temp1[$acc]["debet_ini"]=$r[jml_debet_ini]==""?0:$r[jml_debet_ini];
		$arr_temp1[$acc]["kredit_ini"]=$r[jml_kredit_ini]==""?0:$r[jml_kredit_ini];;
		$arr_temp1[$acc]["saldo_ini"]=$saldo_ini==""?0:$saldo_ini;
	}
	return $arr_temp1;
}
function arr_summ_per_akt_tetap2($bulan,$tahun,$panjang=3){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$str_debet_lalu="dsll + ";
	$str_kredit_lalu="ksll + ";
	for($i=1;$i<=$bulan;$i++){
		$str_debet_ini.="dt_$i + ";
		$str_kredit_ini.="kt_$i + ";
	}
	$str_debet_ini=substr($str_debet_ini,0,strlen($str_debet_ini)-3);
	$str_kredit_ini=substr($str_kredit_ini,0,strlen($str_kredit_ini)-3);
	if($str_debet_ini==""){
		$str_debet_ini="0";
	}
	if($str_kredit_ini==""){
		$str_kredit_ini="0";
	}
	$bulan=trim(str_replace("0","",$bulan));	
	$str_debet=$str_debet_lalu.$str_debet_ini;
	$str_kredit=$str_kredit_lalu.$str_kredit_ini;

	$sql2="select substr(account_kode,1,$panjang) as account_kode,sum(dsll) as jml_debet_lalu,sum(ksll) as jml_kredit_lalu,sum($str_debet) as jml_debet,sum($str_kredit) as jml_kredit,sum($str_debet_ini) as jml_debet_ini,sum($str_kredit_ini) as jml_kredit_ini from tbl_summary_account where tahun='$tahun' ".sql_account($cdivisi_account)." and account_kode like '18%.%' and account_kode not like '%.%%%0' group by substr(account_kode,1,$panjang) order by substr(account_kode,1,$panjang)";
	$res2=dbresult($sql2);//echo $sql;
	//echo $sql2."<br>";
	while($r2=pg_fetch_array($res2)){
		$acc2=$r2[account_kode];
		$kode_awal2=$acc2;
		$saldo_lalu2=strpos($tanda_account,$kode_awal2)>0?$r2[jml_kredit_lalu]-$r2[jml_debet_lalu]:$r2[jml_debet_lalu]-$r2[jml_kredit_lalu];
		$saldo_ini2=strpos($tanda_account,$kode_awal2)>0?$r2[jml_kredit_ini]-$r2[jml_debet_ini]:$r2[jml_debet_ini]-$r2[jml_kredit_ini];
		$saldo2=strpos($tanda_account,$kode_awal2)>0?($r2[jml_kredit]-$r2[jml_debet]):($r2[jml_debet]-$r2[jml_kredit]);
		$arr_temp2[$acc2]["saldo"]=$saldo2==""?0:$saldo2;
		$arr_temp2[$acc2]["debet"]=$r2[jml_debet]==""?0:$r2[jml_debet];
		$arr_temp2[$acc2]["kredit"]=$r2[jml_kredit]==""?0:$r2[jml_kredit];
		$arr_temp2[$acc2]["debet_lalu"]=$r2[jml_debet_lalu]==""?0:$r2[jml_debet_lalu];
		$arr_temp2[$acc2]["kredit_lalu"]=$r2[jml_kredit_lalu]==""?0:$r2[jml_kredit_lalu];;
		$arr_temp2[$acc2]["saldo_lalu"]=$saldo_lalu2==""?0:$saldo_lalu2;
		$arr_temp2[$acc2]["debet_ini"]=$r2[jml_debet_ini]==""?0:$r2[jml_debet_ini];
		$arr_temp2[$acc2]["kredit_ini"]=$r2[jml_kredit_ini]==""?0:$r2[jml_kredit_ini];;
		$arr_temp2[$acc2]["saldo_ini"]=$saldo_ini2==""?0:$saldo_ini2;
	}
	/*	$sql3="select account_kode from tbl_account where account_kode like '18%' ".sql_account($cdivisi_account)." group by account_kode order by account_kode";
		$res3=dbresult($sql3);//echo $sql;
	//echo $sql3."<br>";
	while($r3=pg_fetch_array($res3,0)){
	$acc3=$r3[account_kode];
	$kode_akhir=substr($acc,-1,1);
	$sub_acc3=substr($acc3,0,3);
	$arr_temp[$acc3]["saldo_lalu"]=$arr_temp2[$acc3]["saldo_lalu"];
	$arr_temp[$acc3]["saldo"]=$arr_temp1[$acc3]["saldo"];
	$arr_temp[$acc3]["debet"]=$arr_temp1[$acc3]["debet"]-$arr_temp2[$acc3]["debet"];
	$arr_temp[$acc3]["kredit"]=$arr_temp2[$acc3]["debet"]-$arr_temp2[$acc3]["kredit"];
	}
	 */	return $arr_temp2;
}

function nggawe_halaman_buku($max_record,$arr_baris,$jenis2,$jenis3,$jenis4,$jenis5="tanda_tangan"){
	global $cdivisi_account;
	global $tanda_account,$tanda_lb;
	$record=$max_record;
	$baris_ideal=($record*ceil(count($arr_baris)/$record))+10;
	$record_min_2=$record-2;
	$record_min_1=$record-1;
	echo $arr_baris[0][isi];
	$pr_baris=1;
	for($ib=1;$ib<=$baris_ideal;$ib++){
		$print_n=true;	
		//if(($ib % 43==0)or($ib % 44==0)){
		//echo str_replace('xxx','yyy',$arr_baris[0][isi]);
		//}else{
		if(($ib % $record_min_2==0)or($ib % $record_min_1==0)or($ib % $record_min_3==1)or($ib % $record==0)){
			if(($ib % $record_min_2==0)){
				$jml_chek=3;
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis4){
					for($jb=1;$jb<=$jml_chek;$jb++){
						//echo $arr_baris[0][isi];
						//echo $arr_baris[-1][isi];
						echo str_replace('jamal','jamal 1',$arr_baris[-1][isi]);
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}
			}elseif(($ib % $record_min_1==0)){
				$jml_chek=2;
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis3){
					for($jb=1;$jb<=$jml_chek;$jb++){
						//echo $arr_baris[0][isi];
						echo str_replace('jamal','jamal 3',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}
			}elseif(($ib % $record==0)){
				$jml_chek=1;
				//echo " test $pr_baris -- ".$arr_baris[$pr_baris+$jml_chek][jenis]."<br>";
				if($arr_baris[$pr_baris+$jml_chek][jenis]==$jenis2){
					for($jb=1;$jb<=$jml_chek;$jb++){
						echo str_replace('jamal','jamal 4',$arr_baris[-1][isi]);
						//echo $arr_baris[-1][isi];
						//$pr_baris--;
						$ib++;
					}
					$print_n=false;
				}
			}

			if($print_n){
				//echo "$ib -- $pr_baris -- ".$arr_baris[$pr_baris][isi];
				//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
				echo $arr_baris[$pr_baris][isi];
				$pr_baris++;
			}
		}else{
			//echo str_replace('&nbsp;',"$ib",$arr_baris[$pr_baris][isi]);
			echo $arr_baris[$pr_baris][isi];
			$pr_baris++;
		}
		//}
	}
	return $arr_baris;
	//echo $arr_baris[$pr_baris][isi];
}

function baca_waktu(){
	$waktu_awal=mktime(date("h"),date("i"),date("s"),date("m"),date("j"),date("Y"));
	return $waktu_awal;
}

function normalisasi_transaksi($bulan,$tahun,$sql_plus=""){
	global $cdivisi_account,$cdivisi_kode,$cnospk;
	$sql_update="";
	$sql_update.="update tbl_debet_kredit set proyek='' where proyek is null and tahun='$tahun' and date_part('month',periode_posting)=$bulan $sql_plus ".sql_divisi_kode2().";";
	//dbresult($sql_update_kp);//echo $sql_update_kp;
	$sql_update.="update tbl_debet_kredit set kode_customer='' where kode_customer is null and tahun='$tahun' and date_part('month',periode_posting)=$bulan $sql_plus ".sql_divisi_kode2().";";
	//dbresult($sql_update_kc);//echo $sql_update_kc;
	$sql_update.="update tbl_debet_kredit set kode_cc='0000000000' where tahun='$tahun' and date_part('month',periode_posting)=$bulan and (kode_cc is null or kode_cc='') $sql_plus ".sql_divisi_kode2().";";
	//dbresult($sql_update_kcc);//echo $sql_update_kcc;
	$sql_update.="update tbl_debet_kredit set mu='RP' where tahun='$tahun' and trim(mu)='RP' and date_part('month',periode_posting)=$bulan $sql_plus ".sql_divisi_kode2().";";
	$sql_update.="delete from tbl_summary_account where tahun='$tahun' and (account_kode='' or account_kode is null) $sql_plus ".sql_divisi_kode2().";";
	$sql_update.="delete from tbl_summary_account2 where tahun='$tahun' and (account_kode='' or account_kode is null) $sql_plus ".sql_divisi_kode2().";";
	dbresult($sql_update);//echo $sql_update_murp;
	$sql_update="";
}

function generate_transaksi($bulan,$tahun,$sql_plus=""){
	global $cdivisi_account,$cdivisi_kode,$cnospk;
	$sql_update_kp="update tbl_debet_kredit set proyek='' where proyek is null and tahun='$tahun' and date_part('month',periode_posting)=$bulan $sql_plus ".sql_divisi_kode2();
	//dbresult($sql_update_kp);//echo $sql_update_kp;
	$sql_update_kc="update tbl_debet_kredit set kode_customer='' where kode_customer is null and tahun='$tahun' and date_part('month',periode_posting)=$bulan $sql_plus ".sql_divisi_kode2();
	//dbresult($sql_update_kc);//echo $sql_update_kc;
	$sql_update_kcc="update tbl_debet_kredit set kode_cc='0000000000' where tahun='$tahun' and date_part('month',periode_posting)=$bulan and (kode_cc is null or kode_cc='') $sql_plus ".sql_divisi_kode2();
	//dbresult($sql_update_kcc);//echo $sql_update_kcc;
	$sql_update_murp="update tbl_debet_kredit set mu='RP' where tahun='$tahun' and date_part('month',periode_posting)=$bulan and trim(mu)='RP' $sql_plus ".sql_divisi_kode2();
	//dbresult($sql_update_murp);//echo $sql_update_murp;

	$tambah_kode_cc=$cnospk==""?"and kode_cc='0000000000'":"and kode_cc='$cnospk'";
	$sql="select account_kode,jenis,sum(nilai) as jumlah,proyek,kode_customer,kode_cc from tbl_debet_kredit where tahun='$tahun' and date_part('month',periode_posting)=$bulan $tambah_kode_cc $sql_plus ".sql_divisi_kode2()." group by proyek,account_kode,jenis,kode_customer,kode_cc order by account_kode,proyek,kode_customer,jenis,kode_cc";
	$res=dbresult($sql);
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$kode=$r[account_kode];
		$proyek=$r[proyek];
		$kode_customer=$r[kode_customer];
		$kode_cc=$r[kode_cc];		
		$jenis=$r[jenis]=="1"?"D":"K";
		$arr[$proyek][$kode_customer][$kode_cc][$kode][$jenis]=$r[jumlah];
	}
	return $arr;
}

function generate_transaksi_non_rp($bulan,$tahun){
	global $cdivisi_account,$cdivisi_kode,$cnospk;
	$tambah_kode_cc=$cnospk==""?"and kode_cc='0000000000'":"and kode_cc='$cnospk'";
	$sql="select account_kode,jenis,sum(nilai) as jumlah,proyek,kode_customer,mu,kode_cc from tbl_debet_kredit where tahun='$tahun' and date_part('month',periode_posting)=$bulan and trim(mu)<>'RP' $tambah_kode_cc ".sql_divisi_kode2()." group by proyek,account_kode,jenis,kode_customer,mu,kode_cc order by account_kode,proyek,kode_customer,jenis,mu,kode_cc";
	$res=dbresult($sql);
	//echo $sql."<br>";
	while($r=pg_fetch_array($res)){
		$kode=$r[account_kode];
		$proyek=$r[proyek];
		$kode_customer=$r[kode_customer];
		$mu=$r[mu];
		$kode_cc=$r[kode_cc];		
		$jenis=$r[jenis]=="1"?"D":"K";
		$arr[$proyek][$kode_customer][$mu][$kode_cc][$kode][$jenis]=$r[jumlah];
	}
	return $arr;
}

function file_upload_jamal($userfile,$userfile_name,$curdir_upload){
	//global $curdir_upload;
	//global $userfile_name;
	if($userfile!=""){
		if(!is_dir("$curdir_upload/")){
			mkdir("$curdir_upload",0700);
		}

#if(!copy($userfile,"$curdir_upload/$userfile_name")){
#	echo "error cuk $curdir_upload";
#}
		if (!copy($userfile, "$curdir_upload$userfile_name")) { 
			print ("failed to copy $userfile...<br>\n"); 
		}else{ 
# echo "berhasil copy $userfile ke $curdir_gambar$userfile_name<br>";
# echo "$curdir_gambar$userfile_name";
		}
	}
}
function set_menu($username){
#$sql="select * from tbl_autority a,tbl_sub_feature b ,tbl_feature c where b.feature_id=c.feature_id and b.sub_f_id=a.sub_f_id and level_id<>1 and user_name='".$username."' and (sub_f_disabled<>'yes' or sub_f_disabled is null) order by b.sub_f_id";
	$sql="select * from tbl_autority a,tbl_sub_feature b ,tbl_feature c where b.feature_id=c.feature_id and b.sub_f_id=a.sub_f_id and user_name='".$username."' and (sub_f_disabled<>'yes' or sub_f_disabled is null) order by b.sub_f_id";
        //echo $sql;
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arrm[finance][$r[feature_id]][$r[sub_f_id]][nama]=$r[sub_f_name];
		$arrm[finance][$r[feature_id]][$r[sub_f_id]][alamat]=$r[sub_f_link];
		$arrm[finance][$r[feature_id]][$r[sub_f_id]][target]=$r[sub_f_target];
		$arrm[finance][$r[feature_id]][$r[sub_f_id]][induk]=$r[sub_f_induk];
	}
	return $arrm;
}
function set_menu_main($username){
	$sql="select * from tbl_autority_marketing a,tbl_sub_feature_marketing b ,tbl_feature_marketing c where b.feature_id=c.feature_id and b.sub_f_id=a.sub_f_id and level_id<>1 and user_name='".$username."' and (sub_f_disabled<>'yes' or sub_f_disabled is null) order by b.sub_f_id";
	$res=dbresult($sql);
	while($r=pg_fetch_array($res)){
		$arrm[main_menu][$r[feature_id]][$r[sub_f_id]][nama]=$r[sub_f_name];
		$arrm[main_menu][$r[feature_id]][$r[sub_f_id]][alamat]=$r[sub_f_link];
		$arrm[main_menu][$r[feature_id]][$r[sub_f_id]][target]=$r[sub_f_target];
	}
	return $arrm;
}

//untuk yg lama tanpa pembatasan no nota
/*
   function no_nota($tipe,$h,$b,$t,$nospk=""){
   global $cdivisi_kode;
   $btemp=$b;	
   $b=intval($b);

   ($h>15)?$qh=" and date_part('day',tgl_transaksi) >15":$qh=" and date_part('day',tgl_transaksi) <=15";
   ($nospk=="")?$qp="":$qp=" and no_spk='$nospk'";
   $sql="select max((substr(no_transaksi,24,3))::numeric) as no_transaksi from tbl_transaksi where substr(no_transaksi,22,1)='$tipe' and date_part('year',tgl_transaksi)='$t' and date_part('month',tgl_transaksi)='$b' and data_divisi_kode='$cdivisi_kode' $qh $qp";

   $res=dbresult($sql);
   $r=pg_fetch_array($res);
   $nourut=$r[no_transaksi]+1;
   $nospk==""?$nospk="0000000000":$nospk;
   switch($b){
   case "1":
   ($h<=15)?$b="A":$b="B";
   break;
   case "2":
   ($h<=15)?$b="C":$b="D";
   break;
   case "3":
   ($h<=15)?$b="E":$b="F";
   break;
   case "4":
   ($h<=15)?$b="G":$b="H";
   break;
   case "5":
   ($h<=15)?$b="I":$b="J";
   break;
   case "6":
   ($h<=15)?$b="K":$b="L";
   break;
   case "7":
   ($h<=15)?$b="M":$b="N";
   break;
   case "8":
   ($h<=15)?$b="O":$b="P";
   break;
   case "9":
   ($h<=15)?$b="Q":$b="R";
   break;
   case "10":
   ($h<=15)?$b="S":$b="T";
   break;
   case "11":
   ($h<=15)?$b="U":$b="V";
   break;
   case "12":
   ($h<=15)?$b="W":$b="X";
   break;
   default:
   ($h<=15)?$b="A":$b="B";
   break;
   }
   $nourut=str_pad($nourut,3,"0",STR_PAD_LEFT);

   return $cdivisi_kode."-".$nospk."-".substr($t,-2)."-".str_pad($btemp,2,"0",STR_PAD_LEFT)."-".$tipe.$b.$nourut;
   }
 */
function cek_notrans($notabanding){
	global $cnospk;
	$sql="select no_transaksi from tbl_transaksi where no_transaksi='$notabanding' and kode_cc='$cnospk'";
	$res=dbresult($sql);
	if($r=pg_fetch_array($res)){
		/*		echo "<script>alert('No Nota ".substr($notabanding,-5)." sudah terpakai');</script>";
				echo "<script>window.history.back();</script>";
				exit("No Nota ".substr($notabanding,-5)." sudah terpakai");	*/
		$hasil_cek=1;
	}
	return $hasil_cek;
}
function cek_notrans_dk($notabanding){
	global $cnospk;
	$sql="select no_transaksi from tbl_debet_kredit where no_transaksi='$notabanding' and kode_cc='$cnospk'";
	$res=dbresult($sql);
	if($r=pg_fetch_array($res)){
		/*		echo "<script>alert('No Nota ".substr($notabanding,-5)." sudah terpakai');</script>";
				echo "<script>window.history.back();</script>";
				exit("No Nota ".substr($notabanding,-5)." sudah terpakai");	*/
		$hasil_cek=1;
	}
	return $hasil_cek;
}
/*
   function no_nota($tipe,$h,$b,$t,$nospk="",$cek="0",$nota_set=""){
   global $cdivisi_kode,$cnota_awal,$cnota_akhir,$cnospk,$cotoritas;

//jika login divisi maka nomor spk u/ nota nya jadi 0000000000
if($cotoritas=="divisi"){
$nospk = "";
}else{
$nospk = $nospk;
}
$awal = $cnota_awal;
$akhir = $cnota_akhir;
//query nota + cookie dari set nota
if($awal != "" && $akhir != ""){
$qnota =  "and (substr(no_transaksi,24,3)::numeric <= $akhir and substr(no_transaksi,24,3)::numeric >= $awal)";
}
$btemp=$b;
$b=intval($b);
//balias u/kode bulan
switch($b){
case "1":
($h<=15)?$balias="A":$balias="B";
break;
case "2":
($h<=15)?$balias="C":$balias="D";
break;
case "3":
($h<=15)?$balias="E":$balias="F";
break;
case "4":
($h<=15)?$balias="G":$balias="H";
break;
case "5":
($h<=15)?$balias="I":$balias="J";
break;
case "6":
($h<=15)?$balias="K":$balias="L";
break;
case "7":
($h<=15)?$balias="M":$balias="N";
break;
case "8":
($h<=15)?$balias="O":$balias="P";
break;
case "9":
($h<=15)?$balias="Q":$balias="R";
break;
case "10":
($h<=15)?$balias="S":$balias="T";
break;
case "11":
($h<=15)?$balias="U":$balias="V";
break;
case "12":
($h<=15)?$balias="W":$balias="X";
break;
default:
($h<=15)?$balias="A":$balias="B";
break;
}
//untuk yg manual
if($cek!="1"){
$sqlprefix.=" and substr(no_transaksi,22,2) = '".$tipe.$balias."'";
}
//query tanggal
($h>15)?$qh=" and date_part('day',tgl_transaksi) >15":$qh=" and date_part('day',tgl_transaksi) <=15";
//query proyek
($nospk=="")?$qp="":$qp=" and no_spk='$nospk'";
$sql="select max((substr(no_transaksi,24,3))::numeric) as no_transaksi from tbl_transaksi where substr(no_transaksi,22,1)='$tipe' and date_part('year',tgl_transaksi)='$t' and date_part('month',tgl_transaksi)='$b' and data_divisi_kode='$cdivisi_kode' $qh $qp $qnota $sqlprefix";

$res=dbresult($sql);
$r=pg_fetch_array($res);
if($awal != "" && $akhir != "" && $tipe==$nota_set){// jika ada cookie setnota
	if($r[no_transaksi]=="" || $r[no_transaksi]==0){// jika masi kosong, nourut=awal
		$nourut = $awal;
	}elseif($r[no_transaksi]==$akhir){// nota = setnota terakhir
		if($cek!="1"){
			echo "<script>alert('Alokasi Nomor Nota Anda Habis');</script>";
			echo "<script>window.location.href='../master/master-nota.php'</script>";
			exit();
		}else{
			$nourut = $awal;
		}
	}else{
		$nourut = $r[no_transaksi]+1;
	}
}else{
	$nourut = $r[no_transaksi]+1; 
}
$nospk==""?$nospk="0000000000":$nospk; //nospk
$nourut=str_pad($nourut,3,"0",STR_PAD_LEFT); //tambah nol

return $cdivisi_kode."-".$nospk."-".substr($t,-2)."-".str_pad($btemp,2,"0",STR_PAD_LEFT)."-".$tipe.$balias.$nourut;
}
*/

function no_nota($tipe,$h,$b,$t,$nospk="",$cek="0",$nota_set="",$cek_lagi="",$elim=""){
	global $cdivisi_kode,$cnota_b,$cnota_h,$cnota_k,$cnota_m,$cnota_p,$cnospk,$cotoritas;
	/*echo $cnota_b." B OK<br>";
	  echo $cnota_h." H OK<br>";
	  echo $cnota_k." K OK<br>";
	  echo $cnota_m." M OK<br>";
	  echo $cnota_p." P OK<br>";*/
	//jika login divisi maka nomor spk u/ nota nya jadi 0000000000
	//echo $nospk." OK $cotoritas<br>";	
	if($cotoritas=="divisi"){
		$nospk = "0000000000";
	}else{
		$nospk = $nospk;
	}
	$nospk=$elim==""?$nospk:$elim;
	$btemp=$b;
	$b=intval($b);
	//balias u/kode bulan
	switch($b){
		case "1":
			($h<=15)?$balias="A":$balias="B";
		break;
		case "2":
			($h<=15)?$balias="C":$balias="D";
		break;
		case "3":
			($h<=15)?$balias="E":$balias="F";
		break;
		case "4":
			($h<=15)?$balias="G":$balias="H";
		break;
		case "5":
			($h<=15)?$balias="I":$balias="J";
		break;
		case "6":
			($h<=15)?$balias="K":$balias="L";
		break;
		case "7":
			($h<=15)?$balias="M":$balias="N";
		break;
		case "8":
			($h<=15)?$balias="O":$balias="P";
		break;
		case "9":
			($h<=15)?$balias="Q":$balias="R";
		break;
		case "10":
			($h<=15)?$balias="S":$balias="T";
		break;
		case "11":
			($h<=15)?$balias="U":$balias="V";
		break;
		case "12":
			($h<=15)?$balias="W":$balias="X";
		break;
		default:
		($h<=15)?$balias="A":$balias="B";
		break;
	}
	$kc=strtolower($tipe);
	//query nota + cookie dari set nota
	if($kc=="b"){
		$qnota =  "and (substr(no_transaksi,24,3)::numeric=$cnota_b)";
	}elseif($kc=="h"){
		$qnota =  "and (substr(no_transaksi,24,3)::numeric=$cnota_h)";
	}elseif($kc=="k"){
		$qnota =  "and (substr(no_transaksi,24,3)::numeric=$cnota_k)";
	}elseif($kc=="m"){
		$qnota =  "and (substr(no_transaksi,24,3)::numeric=$cnota_m )";
	}elseif($kc=="p"){
		$qnota =  "and (substr(no_transaksi,24,3)::numeric=$cnota_p)";
	}

	//untuk yg manual
	if($cek!="1"){
		$sqlprefix.=" and substr(no_transaksi,22,2) = '".$tipe.$balias."'";
	}
	//query tanggal
	($h>15)?$qh=" and date_part('day',tgl_transaksi) >15":$qh=" and date_part('day',tgl_transaksi) <=15";
	//query proyek
	($nospk=="")?$qp="":$qp=" and kode_cc='$nospk'";
	$sql="select max((substr(no_transaksi,24,3))::numeric) as no_transaksi from tbl_transaksi where substr(no_transaksi,22,1)='$tipe' and date_part('year',tgl_transaksi)='$t' and date_part('month',tgl_transaksi)='$b' and data_divisi_kode='$cdivisi_kode' $qh $qp $sqlprefix";

	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	$nomor_transaksi=$r[no_transaksi];
	$ks="nota_$kc";
	if($kc=="b"){
		$nota_pilih=$cnota_b;
	}elseif($kc=="h"){
		$nota_pilih=$cnota_h;
	}elseif($kc=="k"){
		$nota_pilih=$cnota_k;
	}elseif($kc=="m"){
		$nota_pilih=$cnota_m;
	}elseif($kc=="p"){
		$nota_pilih=$cnota_p;
	}
	//ambil nota + cookie dari set nota
	$tambahan_mandiri=$nospk==""?"and proyek='0000000000'":"and proyek='$nospk'";
	$sql2="select * from tbl_set_nota where divisi_kode='$cdivisi_kode' and tahun='$t' $tambahan_mandiri";
	$r2=pg_fetch_array(dbresult($sql2));	
	$nota_diset=$r2[$ks];
	//	echo $nota_pilih." $nomor_transaksi $nota_diset OK<br>";
	if (($nomor_transaksi) >= ($nota_diset)){
		if($nota_pilih!= ""){// jika ada cookie setnota
			//if($nomor_transaksi!="" && $nomor_transaksi!=0){// jika masih kosong, nourut=awal
			if($nomor_transaksi=="" && $nomor_transaksi!=0){// jika masih kosong, nourut=awal
				$nourut = $nota_pilih;
				//	echo "AKU<br>";
			}else{

				$nota_pilih1=str_pad($nota_pilih,3,"0",STR_PAD_LEFT);
				$no_lama1=$cdivisi_kode."-".$cnospk."-".substr($t,-2)."-".str_pad($btemp,2,"0",STR_PAD_LEFT)."-".$tipe.$balias.$nota_pilih1;
				$not = cari_nilai("select count (no_transaksi) from tbl_transaksi where no_transaksi = '$no_lama1'");

				$nourut=$not!="0"?$nomor_transaksi+1:$nota_pilih;
				//	echo "KOWE<br>";
			}
		}else{
			$nourut = $nomor_transaksi+1; 
			//echo "AWAKMU<br>";
		}
		}else{
			if($nota_pilih!= ""){
				$nourut=$nota_diset;
			}else{
				$nourut = $nomor_transaksi+1;
			}
		}
		//echo $nourut." KETAN<br>";		
		$nourut=$elim!="" && $nourut==1?901:$nourut;
		if($cek_lagi==""){
			$nourut=$nourut;
		}else{
			$nourut=$nourut+1;
		}
		$nospk==""?$nospk="0000000000":$nospk; //nospk
		$nourut=str_pad($nourut,3,"0",STR_PAD_LEFT); //tambah nol

		return $cdivisi_kode."-".$nospk."-".substr($t,-2)."-".str_pad($btemp,2,"0",STR_PAD_LEFT)."-".$tipe.$balias.$nourut;
	}

	function ctr_array($arr){
		echo "<pre>";
		print_r($arr);
		echo "</pre>";
	}

	function pegawai($dp_nip,$tanggal=""){
		$tanggal=$tanggal==""?date('Y-m-d'):$tanggal;
		$sql="select * from tbl_data_pegawai where dp_nip='$dp_nip'";
		$res=dbresult($sql);
		$r=pg_fetch_array($res);
		$dp_gelar_awal=$r[dp_gelar_awal]==""?"":$r[dp_gelar_awal].". ";
		$dp_gelar_akhir=$r[dp_gelar_akhir]==""?"":", ".$r[dp_gelar_akhir];
		$nama=$dp_nip==""?"":"$dp_gelar_awal $r[dp_nama_depan] $r[dp_nama_keluarga]$dp_gelar_akhir";

		$divisi_kode=$r[divisi_kode];
		$divisi=cari_nilai("select divisi_name from tbl_dm_divisi where divisi_kode='$divisi_kode'");	

		$kode_jabatan=$r[kode_jabatan];
		$jabatan=cari_nilai("select nama_jabatan from tbl_dm_jabatan where kode_jabatan='$kode_jabatan'");
		$hasil=array($nama,$divisi_kode,$divisi,$kode_jabatan,$jabatan);
		return $hasil;
	}
	function cariIdePro2($no_bukti){
		$sql="select bkb_customer,no_spk from tbl_fn_buktikasbank where bkb_bukti='$no_bukti'";
		$res=dbresult($sql);
		if($r1=pg_fetch_array($res)){
			return $r;
		}else{
			$r=array("","");
			return $r;
		}
	}

	function cariIdePro($no_bukti){
		$sql="select dbkb_nobukti from tbl_fn_detbuktikasbank where bkb_bukti='$no_bukti'";
		$res=dbresult($sql);
		$r1=pg_fetch_array($res);
		if(is_array($r1)){
			$sql="select a.kode_customer, a.no_spk, b.nama_customer from tbl_fp_daftar_invoice a, tbl_fp_daftar_customer b where a.no_invoice_c='$r1[dbkb_nobukti]' and a.kode_customer=b.kode_customer";
			$res=dbresult($sql);
			$r=pg_fetch_array($res);
			if(is_array($r)){
				return $r;
			}else{
				$r=array("","");
				return $r;
			}
		}else{
			$r=array("","");
			return $r;

		}
	}

	function image($data,$width=""){
		list($lebar) = getimagesize("$data");
		$nilai=$width==""?450:$width;
		$hasil=$lebar>$nilai?$nilai:$lebar;
		$image="<image src=\"$data\" border=\"0\" width=\"$hasil\" class=\"image\">";
		return $image;
	}

	function balik($tanggal){
		$arr=split("/",$tanggal);
		$hasil=$arr[2]."-".$arr[1]."-".$arr[0];
		return $hasil;
	}

	function head($title){
		$txt_out.="<table width=\"98%\" height=\"35\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
			<tr> 
			<td height=\"5\" colspan=\"2\"><img src=\"../img/0.gif\" width=\"1\" height=\"1\"></td>
			</tr>
			<tr> 
			<td width=\"25\" class=\"bgjdl\"><img src=\"../img/logo-adhi.gif\" width=\"20\" height=\"20\"></td>
			<td height=\"25\" class=\"bgjdl\">$title</td>
			</tr>
			<tr> 
			<td height=\"5\" colspan=\"2\"><img src=\"../img/0.gif\" width=\"1\" height=\"1\"></td>
			</tr>
			</table>";
		return $txt_out;
	}

	function subbidang_kode($subbidang_kode){
		$panjang=strlen($subbidang_kode);
		$i=$panjang-1;
		while($i>=0 and $batas==""){
			$nilai=substr($subbidang_kode,$i,1);
			$batas=$nilai==0?"":"x";
			$isi+=$nilai==0?1:0;
			$i--;
		}
		$long=$panjang-$isi;
		$hasil=substr($subbidang_kode,0,$long);
		return $hasil;
	}

	function combo_menu_buku_biru($nospk){
		global $inp_par;
		//  $arr_bab=array("-- Pilih Dokumen --","BAB I -- MANAJEMEN PROYEK TERPADU","BAB II -- MANAJEMEN LINGKUP PROYEK","BAB III -- MANAJEMEN WAKTU PROYEK","BAB IV -- MANAJEMEN BIAYA PROYEK","BAB V -- MANAJEMEN MUTU PROYEK","BAB VI -- MANAJEMEN SDM PROYEK","BAB VII -- MANAJEMEN KOMUNIKASI PROYEK","BAB VIII -- MANAJEMEN RESIKO PROYEK","BAB IX -- MANAJEMEN PENGADAAN PROYEK","BAB X -- MANAJEMEN KESELAMATAN KERJA","BAB XI -- MANAJEMEN LINGKUNGAN","BAB XII -- MANAJEMEN KEUANGAN","BAB XIII -- MANAJEMEN KLAIM");
		$arr_bab=array("0"=>"-- Pilih Dokumen --",
				"1"=>"BAB I -- MANAJEMEN PROYEK TERPADU",
				"2"=>"BAB II -- MANAJEMEN LINGKUP PROYEK",
				"3"=>"BAB III -- MANAJEMEN WAKTU PROYEK",
				"4"=>"BAB IV -- MANAJEMEN BIAYA PROYEK",
				"5"=>"BAB V -- MANAJEMEN MUTU PROYEK",
				"6"=>"BAB VI -- MANAJEMEN SDM PROYEK",
				"7"=>"BAB VII -- MANAJEMEN KOMUNIKASI PROYEK",
				"8"=>"BAB VIII -- MANAJEMEN RESIKO PROYEK",
				"9"=>"BAB IX -- MANAJEMEN PENGADAAN PROYEK",
				"10"=>"BAB X -- MANAJEMEN KESELAMATAN KERJA",
				"11"=>"BAB XI -- MANAJEMEN LINGKUNGAN",
				"12"=>"BAB XII -- MANAJEMEN KEUANGAN",
				"13"=>"BAB XIII -- MANAJEMEN KLAIM");


		$arr_isi[0][]="Berita Acara Serah Terima Buku Perencanaan Proyek (Book Plan)";
		$arr_isi[0][]="Berita Acara Pengesahan Buku Perencanaan Proyek (Book Plan)";
		$arr_isi[0][]="Surat Penugasan Team Pembuatan Buku Perencanaan Proyek (Book Plan)";
		$arr_isi[0][]="Berita Acara Serah Terima Buku Merah";
		$arr_isi[0][]="Rekap Biaya Ada Tanda Tangan Kadiv";



		$arr_isi[1][]="Data Teknik";
		$arr_isi[1][]="Data Keuangan";
		$arr_isi[1][]="Informasi Pemilik Proyek, Konsultan Dan Kontraktor";
		$arr_isi[1][]="Informasi Kontrak";

		$arr_isi[2][]="Lingkup Pekerjaan";
		$arr_isi[2][]="Rencana Tata Letak";
		$arr_isi[2][]="Definisi Aktifitas";
		$arr_isi[2][]="Rencana Engineering";
		$arr_isi[2][]="Daftar Submittal";
		$arr_isi[2][]="Overall Charge Control Akibat Perubahan Volume Kontrak";
		$arr_isi[2][]="Checklist Survey Ulang";

		$arr_isi[3][]="Ringkasan Jadwal Induk Pelaksanaan Proyek";
		$arr_isi[3][]="Flow Chart Kegiatan Pelaksanaan Proyek";
		$arr_isi[3][]="Daftar Barang / Bahan dan Jadwal Kebutuhan Bahan";
		$arr_isi[3][]="Daftar Alat Kerja dan Jadwal Kebutuhan Alat";
		$arr_isi[3][]="Daftar Sub Kontraktor dan Jadwal Kebutuhan Sub Kontraktor";

		$arr_isi[4][]="Rekapitulasi Biaya RAP";
		$arr_isi[4][]="Data Volume Pekerjaan RAB";
		$arr_isi[4][]="Analisa Harga Satuan";
		$arr_isi[4][]="Analisa Harga Satuan Pekerjaan";
		$arr_isi[4][]="Daftar Biaya Upah";
		$arr_isi[4][]="Daftar Biaya Bahan";
		$arr_isi[4][]="Daftar Biaya Alat";
		$arr_isi[4][]="Daftar Biaya Sub Kontraktor";
		$arr_isi[4][]="Daftar Biaya Umum Proyek";

		$arr_isi[5][]="Kebijakan Mutu Dan K3L";
		$arr_isi[5][]="Isi";

		$arr_sub[5][1][]="1. Pendahuluan";
		$arr_sub[5][1][]="2. Penjelasan Tentang Sistem Mutu";
		$arr_sub[5][1][]="3. Ringkasan Spesifikasi";
		$arr_sub[5][1][]="4. Rencana Inspeksi dan Pengujian (ITP's)";
		$arr_sub[5][1][]="5. Daftar Gambar (yang digunakan sebagai acuan)";
		$arr_sub[5][1][]="6. Identifikasi Barang dan Produk";
		$arr_sub[5][1][]="7. Teknik Statistik yang Diterapkan";

		$arr_isi[6][]="Struktur Organisasi";
		$arr_isi[6][]="Distribusi Tanggung Jawab";
		$arr_isi[6][]="Perhitungan Job Value Petugas Proyek";
		$arr_isi[6][]="SKI Personil Inti";
		$arr_isi[6][]="Bimbingan dan Penilaian Karya";
		$arr_isi[6][]="Uraian Tugas dan Jabatan";

		$arr_isi[7][]="Mekanisme / Sistem Proses Rencana dan Opname Prestasi Lapangan";

		$arr_isi[8][]="Identifikasi dan Treatment of Risk";

		$arr_isi[9][]="Sistem / Mekanisme Kelogistikan";
		$arr_isi[9][]="Sistem / Mekanisme Pergudangan";
		$arr_isi[9][]="Flow Diagram Proses Penagihan Sub Kontraktor / Vendor";
		$arr_isi[9][]="Daftar Peralatan";

		$arr_isi[10][]="Langkah Pengendalian K3";
		$arr_isi[10][]="Struktur Organisasi P2K3L";
		$arr_isi[10][]="Struktur Organisasi Tim Keadaan Darurat dan P3K";
		$arr_isi[10][]="Flow Koordinasi K3";
		$arr_isi[10][]="Identifikasi dan Pengendalian Bahaya";
		$arr_isi[10][]="Data Instansi Terkait";
		$arr_sub[10][0][]="A. Pengendalian Awal";
		$arr_sub[10][0][]="B. Pengendalian Saat Kontrak dengan Pekerja";
		$arr_sub[10][0][]="C. Pengendalian Sesudah Kontrak dengan Pekerja";


		$arr_isi[11][]="Koordinasi Lingkungan";
		$arr_isi[11][]="Pokok Perhatian K3";
		$arr_sub[11][0][]="A. Lingkungan Fisik";
		$arr_sub[11][0][]="B. Lalu Lintas";
		$arr_sub[11][0][]="C. Lain-Lain";

		$arr_isi[12][]="Rencana Cash Flow";

		$txtout.="<select name=\"inp[menu_$nospk]\" class=\"form2\" onChange=pindah('$nospk');>\n";

		if(is_array($arr_bab)){
			while(list($key,$bab)=each($arr_bab)){
				$select=trim($key)===trim($inp_par[menu])?"selected":"";
				$txtout.="<option value='$key' $select>$bab</option>\n";
				if(is_array($arr_isi[$key])){
					while(list($no,$nilai)=each($arr_isi[$key])){
						$select=trim($nilai)===trim($inp_par[menu])?"selected":"";
						$txtout.="<option value='$nilai' $select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$nilai</option>\n";
						if(is_array($arr_sub[$key][$no])){
							while(list($det,$isi)=each($arr_sub[$key][$no])){
								$select=trim($isi)===trim($inp_par[menu])?"selected":"";
								$txtout.="<option value='$isi' $select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$isi</option>\n";
							}
						}

					}
				}
			}
		}

		$txtout.="</select>\n";
		return $txtout;

	}

	function combo_buku_biru($nospk,$status=""){
		global $inp_par;
		$arr_buku_biru=$status==""?
			array(
					"biru-permohonan"=>"Surat Pemohonan",
					"biru-kontrak"=>"Kontrak",
					"biru-penunjukan"=>"Surat Penunjukan",
					"biru-spk"=>"SPK/SPMK",
					"biru-teknik"=>"Data Teknik",
					"biru-keuangan"=>"Data Keuangan",
					"biru-rab-rap"=>"Data RAB & RAP",
					"biru-lingkup"=>"Lingkup Pekerjaan Utama",
					"biru-org"=>"Struktur Organisasi Proyek",
					"biru-personel"=>"Susunan Personel Inti Proyek",
					"biru-upah"=>"Rencana Kebutuhan Upah",
					"biru-bahan"=>"Rencana Kebutuhan Bahan",
					"biru-alat"=>"Rencana Kebutuhan Alat",
					"biru-subkon"=>"Daftar Subkontraktor",
					"biru-cf"=>"Rencana Cash Flow",
					"biru-rekap-rap"=>"Rekap RAP",
					"biru-metode"=>"Metode Kerja",
					"biru-pernyataan"=>"Surat Pernyataan",
					"biru-buku"=>"Buku Biru",
					"biru-upload"=>"Dokumen Rekap RAB & RAP"
						)
						:
						array(
								""=>"-- Pilih Dokumen --",
								"biru-permohonan"=>"Surat Pemohonan",
								"biru-kontrak"=>"Kontrak",
								"biru-penunjukan"=>"Surat Penunjukan",
								"biru-spk"=>"SPK/SPMK",
								"biru-teknik"=>"Data Teknik",
								"biru-keuangan"=>"Data Keuangan",
								"biru-rab-rap"=>"Data RAB & RAP",
								"biru-lingkup"=>"Lingkup Pekerjaan Utama",
								"biru-org"=>"Struktur Organisasi Proyek",
								"biru-personel"=>"Susunan Personel Inti Proyek",
								"biru-upah"=>"Rencana Kebutuhan Upah",
								"biru-bahan"=>"Rencana Kebutuhan Bahan",
								"biru-alat"=>"Rencana Kebutuhan Alat",
								"biru-subkon"=>"Daftar Subkontraktor",
								"biru-cf"=>"Rencana Cash Flow",
								"biru-rekap-rap"=>"Rekap RAP",
								"biru-metode"=>"Metode Kerja",
								"biru-pernyataan"=>"Surat Pernyataan",
								"biru-buku"=>"Buku Biru",
								"biru-upload"=>"Dokumen Rekap RAB & RAP"
									)
									;
		$hasil=combo_array_new("inp[menu_$nospk]",$arr_buku_biru,$inp_par[menu],"onChange=pindah('$nospk');");
		return $hasil;
	}


	function kode_analisa_dasar(){
		$jumlah=cari_nilai("select max(substr(detail_material_kode,length(detail_material_kode)-4,5)) as \"jumlah\" from tbl_detail_material where detail_material_kode like 'K-%'");
		$kode=strlen($jumlah+1);
		if ($kode==1){
			$kode="0000".($jumlah+1);
		}else if($kode==2){
			$kode="000".($jumlah+1);
		}else if($kode==3){
			$kode="00".($jumlah+1);
		}else if($kode==4){
			$kode="0".($jumlah+1);
		}else if($kode==5){
			$kode=($jumlah+1);
		}else{
			$kode=$jumlah;
		}
		$hasil="K-".$kode;
		return $hasil;
	}

	function longNumber($numx,$numy,$sign,$precNum=0){
		$numx = str_replace(".","",$numx);
		$numy = str_replace(".","",$numy);

		$num = $precNum;
		switch($sign){
			case "+":
				$nilai = gmp_add($numx,$numy);
			break;
			case "-":
				$nilai = gmp_sub($numx,$numy);
			break;
			case "*":
				$nilai = gmp_mul($numx,$numy);
			$num *= 2;
			break;
			case "/":
				$nilai = gmp_div($numx,$numy);
			break;
		}
		$sum = gmp_strval($nilai);
		$val = substr($sum,0,strlen($sum)-$num);
		$pre = substr($sum,strlen($sum)-$num);    

		$string = "$val.$pre";
		return $string;
	}

	function evalsum($sum){
		$to_eval="\$evalled=" . $sum . ";";
		eval($to_eval);
		$hasil=number_format($evalled,3);
		//echo number_format(9.11111, 2)."<br>";
		return $hasil;
	}
	function comboDate($tanggal,$arNama,$blank=""){
		if($tanggal == "") $tanggal = date('Y-m-d');
		IF($blank != "") $tanggal = "yyyy-mm-dd";
		$tgl = explode("-",$tanggal);
		$combo = combo_tanggal("$arNama[0]","$tgl[2]",1,31,"dd")." ";
		$combo .= combo_bulan("$arNama[1]","$tgl[1]","mm")." ";
		$combo .= combo_tanggal("$arNama[2]","$tgl[0]",1920,date("Y")+7,"yyyy");
		return $combo;
	}
	function var2date($arr){
		$arr[0]=str_repeat("0",2-strlen($arr[0])).$arr[0];
		$arr[1]=str_repeat("0",2-strlen($arr[1])).$arr[1];
		$date=$arr[2]."-".$arr[1]."-".$arr[0];
		return $date;
	}
	function tanggal_set($nospk,$bulan,$tahun){
#$sql="select tgl_approve from tbl_approve where no_spk='$nospk' and tgl_approve<='$tahun-$bulan-01' order by tgl_approve desc";
		$sql="select tahap_tanggal_kendali from tbl_tahap_kendali where no_spk='$nospk' and tahap_tanggal_kendali<='$tahun-$bulan-01' order by tahap_tanggal_kendali desc";
		$res=dbresult($sql);//echo "$sql<br>";
		$r=pg_fetch_array($res);
		$arr_tgl_set=$r[tahap_tanggal_kendali];
		return $arr_tgl_set; 
	}

	function checkupd($tgl1,$tgl2){
		$arTgl1=split("-",$tgl1);
		$arTgl2=split("-",$tgl2);
		if(($arTgl1[0]==$arTgl1[0] && $arTgl1[1]==$arTgl1[1])){
			$stat1 = ($arTgl1[2] <= 15)?0:1;
			$stat2 = ($arTgl2[2] <= 15)?0:1;
			if($stat1 != $stat2){
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	}

	$arr_kel_bidang[0]="UPAH";
	$arr_kel_bidang[1]="BAHAN";
	$arr_kel_bidang[2]="ALAT";
	$arr_kel_bidang[3]="SUBKONTRAKTOR";
	$arr_kel_bidang[4]="BIAYA UMUM";

	function filter_kel_bidang($key, $field="subbidang_kode"){
		switch($key){
			case 0:
				$filter="and $field = '16a02010'";
				break;
			case 1:
				$filter="and $field like '12%'";
				break;
			case 2:
				$filter="and $field like '16%' and subbidang_kode != '16a02010'";
				break;
			case 3:
				$filter="";
				break;
			case 4:
				$filter="and $field like '17%'";
				break;
		}		
		return $filter;
	}
	$arr_biayabeban[0]="BUKP";
	$arr_biayabeban[1]="BUKD";
	$arr_biayabeban[2]="BUNGA BANK";

	function cari_volume_total($kode,$arr_tahap){
		$arr_kode=split("\.","$kode");
		$hasil=count($arr_kode);
		$vol_total=1;
		for($i=$hasil-1;$i>=0;$i--){
			for($j=0;$j<=$i;$j++){
				$kode_.="$arr_kode[$j].";
			}
			$kode_=substr($kode_,0,strlen($kode_)-1);
			$arr=split("\t",$arr_tahap[$kode_]);
			$vol_total=$vol_total*$arr[0];
			$kode_="";
		}
		return $vol_total;
	}


	function update_tahap($tahap_kode){
		global $inp_par,$cnospk_pilih;
		$sql="select sum(tahap_total_rab) as total_rab, sum(tahap_valas_rab) as valas_rab,sum(tahap_total_rap) as total_rap, sum(tahap_valas_rap) as valas_rap from tbl_tahap where tahap_kode_induk='$tahap_kode' and no_spk='$cnospk_pilih' and tahap_tanggal='$inp_par[tahap_tanggal]'";	
		$res=dbresult($sql);
		$r=pg_fetch_array($res);
#echo $sql."<br>";
		$r[total_rab]=$r[total_rab]==""?0:$r[total_rab];
		$r[valas_rab]=$r[valas_rab]==""?0:$r[valas_rab];
		$r[total_rap]=$r[total_rap]==""?0:$r[total_rap];
		$r[valas_rap]=$r[valas_rap]==""?0:$r[valas_rap];

		$sql_="update tbl_tahap set tahap_harga_satuan_rab=$r[total_rab],tahap_total_rab=$r[total_rab]*tahap_volume_rab,tahap_harga_valas_rab=$r[valas_rab],tahap_valas_rab=$r[valas_rab]*tahap_volume_rab,tahap_harga_satuan_rap=$r[total_rap],tahap_total_rap=$r[total_rap]*tahap_volume_rap,tahap_harga_valas_rap=$r[valas_rap],tahap_valas_rap=$r[valas_rap]*tahap_volume_rap where tahap_kode='$tahap_kode' and no_spk='$cnospk_pilih' and tahap_tanggal='$inp_par[tahap_tanggal]'";
#echo $sql_."<br>";
		dbresult($sql_);
		$tahap_kode_induk=substr( $tahap_kode,0,strrpos($tahap_kode,"."));
		if($tahap_kode_induk!=""){
			update_tahap($tahap_kode_induk);
		}
	}

	function update_tahap_kontrol($tahap_kontrol_kode){
		global $inp_par,$cnospk_pilih;
		$sql="select sum(tahap_kontrol_total_rab) as total_rab, sum(tahap_kontrol_valas_rab) as valas_rab,sum(tahap_kontrol_total_rap) as total_rap, sum(tahap_kontrol_valas_rap) as valas_rap from tbl_tahap_kontrol where tahap_kontrol_kode_induk='$tahap_kontrol_kode' and no_spk='$cnospk_pilih' and tahap_kontrol_tanggal='$inp_par[tahap_kontrol_tanggal]'";	
		$res=dbresult($sql);
		$r=pg_fetch_array($res);
#echo $sql."<br>";
		$r[total_rab]=$r[total_rab]==""?0:$r[total_rab];
		$r[valas_rab]=$r[valas_rab]==""?0:$r[valas_rab];
		$r[total_rap]=$r[total_rap]==""?0:$r[total_rap];
		$r[valas_rap]=$r[valas_rap]==""?0:$r[valas_rap];

		$sql_="update tbl_tahap_kontrol set tahap_kontrol_harga_satuan_rab=$r[total_rab],tahap_kontrol_total_rab=$r[total_rab]*tahap_kontrol_volume_rab,tahap_kontrol_harga_valas_rab=$r[valas_rab],tahap_kontrol_valas_rab=$r[valas_rab]*tahap_kontrol_volume_rab,tahap_kontrol_harga_satuan_rap=$r[total_rap],tahap_kontrol_total_rap=$r[total_rap]*tahap_kontrol_volume_rap,tahap_kontrol_harga_valas_rap=$r[valas_rap],tahap_kontrol_valas_rap=$r[valas_rap]*tahap_kontrol_volume_rap where tahap_kontrol_kode='$tahap_kontrol_kode' and no_spk='$cnospk_pilih' and tahap_kontrol_tanggal='$inp_par[tahap_kontrol_tanggal]'";
#echo $sql_."<br>";
		dbresult($sql_);
		$tahap_kontrol_kode_induk=substr( $tahap_kontrol_kode,0,strrpos($tahap_kontrol_kode,"."));
		if($tahap_kontrol_kode_induk!=""){
			update_tahap_kontrol($tahap_kontrol_kode_induk);
		}
	}

	function kurs_valas($nilai=""){
		global $cnospk_pilih,$inp_par;
		$sql="select * from tbl_valas where no_spk='$cnospk_pilih' and tahap_tanggal='$inp_par[tahap_tanggal]'";
		$res=dbresult($sql);
		$r=pg_fetch_array($res);
		$hasil=array("$r[kode_matauang]","$r[nilai_kurs]");
		return $hasil;
	}

	function tahap_padding($kode){
		$panjang=strlen($kode);
		$i=$panjang-1;
		while($i>=0 and $tanda==""){
			$nilai=substr($kode,$i,1);
			$tanda=$nilai==0?"":"t";
			$angka+=$nilai==0?1:0;		
			$i--;
		}
		$hasil=($panjang-$angka)*5;
		return $hasil;
	}

	$arr_pembayaran=array("Tunai", "Hutang Kwitansi", "Hutang Belum Kwitansi", "Loonstaat", "Pemindahan", "Pembebanan", "RK");
	function absview_nilai($nilai, $decimal){
		global $mode;
#mode adalah biasanya dipakai sebagai mode export pada laporan
		if( round($nilai, 5)==0 ){
			$hasil = "&nbsp;";
		}else{
			if( $mode=="export" ){
				$hasil=$nilai;
			}else{
				$hasil = round($nilai, 5)<0?"(".number_format(abs($nilai), $decimal).")":number_format($nilai, $decimal);
			}
		}
		return $hasil;
	}
	function hs_terakhir($lokasi, $periode, $kode, $c_m){
		global $cnomor_proyek;
		$sql="select * from \"Trs\" where \"Trs_pry\"='$cnomor_proyek' and \"Trs_a\"='$lokasi' and \"Trs_per\"='$periode' and \"Trs_kode\"='$kode' and \"Trs_m\"='$c_m' order by \"Trs_no\" desc";
		$res_trs=dbresult($sql);
		$sql="select * from \"Perubahan\" where \"Ubh_pry\"='$cnomor_proyek' and \"Ubh_a\"='$lokasi' and \"Ubh_per\"='$periode' and \"Ubh_kode\"='$kode' and \"Ubh_m\"='$c_m'";
		$res_ubh=dbresult($sql);
		if( pg_num_rows($res_trs)==0 && pg_num_rows($res_ubh)==0 ){
			if( $periode == 1 ){
				$sql="select * from cost_c where c_pry='$cnomor_proyek' and c_a='$lokasi' and c_kode='$kode' and c_m='$c_m'";
				$result=dbresult($sql);
				$data=pg_fetch_array($result);
				if( substr($kode, -1)=="0" ){
					$tmp_hs=$data[c_r0bh]==""?0:$data[c_r0bh];
				}else{
					$tmp_hs=$data[c_r0ph]==""?0:$data[c_r0ph];
				}
			}else{
				$sql="select * from \"Cost_real\" where \"R_pry\"='$cnomor_proyek' and \"R_a\"='$lokasi' and \"R_per\" < '$periode' and \"R_item\"='$kode' and \"R_m\"='$c_m' order by length(\"R_per\") desc, \"R_per\" desc limit 1";
				$result=dbresult($sql);
				$data=pg_fetch_array($result);
				if( substr($kode, -1)=="0" ){
					$tmp_hs=$data[Rps_bv]==0?0:$data[Rps_bj]/$data[Rps_bv];
				}else{
					$tmp_hs=$data[R_x];
				}
			}
		}else{
			$data_trs=pg_fetch_array($res_trs);
			$tmp_hs_trs=$data_trs[Trs_vol]==0?0:($data_trs[Trs_kurs]*$data_trs[Trs_jumlah])/$data_trs[Trs_vol];
			$data_ubh=pg_fetch_array($res_ubh);
			$tmp_hs_ubh=$data_ubh[Ubh_x];
			if( pg_num_rows($res_trs)!=0 && pg_num_rows($res_ubh)==0 ){
				$tmp_hs=$tmp_hs_trs;
			}elseif(pg_num_rows($res_ubh)!=0 && pg_num_rows($res_trs)==0){
				$tmp_hs=$tmp_hs_ubh;
			}else{
				if( $data_trs[tanggal_add] > $data_ubh[tanggal_add] ){
					$tmp_hs=$tmp_hs_trs;
				}else{
					if( $data_trs[tanggal_add] == $data_ubh[tanggal_add] ){
						if( $data_trs[waktu_add] > $data_ubh[waktu_add] ){
							$tmp_hs=$tmp_hs_trs;
						}else{
							$tmp_hs=$tmp_hs_ubh;
						}
					}else{
						$tmp_hs=$tmp_hs_ubh;
					}
				}
			}
		}

		/*
		   $sql="select * from \"Cost_real\" where \"R_pry\"='$cnospk_pilih' and \"R_a\"='$lokasi' and \"R_per\" < '$periode' and \"R_item\"='$kode' and \"R_m\"='$c_m' order by length(\"R_per\") desc, \"R_per\" desc limit 1";
		   $result=dbresult($sql);
		   $data=pg_fetch_array($result);
		   if( substr($kode, -1)=="0" ){
		   $tmp_hs=$data[Rps_bv]==0?0:$data[Rps_bj]/$data[Rps_bv];
		   }else{
		   $tmp_hs=$data[R_x];
		   }

		   if( $tmp_hs=="" ){
		   $sql="select * from cost_c where c_pry='$cnospk_pilih' and c_a='$lokasi' and c_kode='$kode' and c_m='$c_m'";
		   $result=dbresult($sql);
		   $data=pg_fetch_array($result);
		   if( substr($kode, -1)=="0" ){
		   $tmp_hs=$data[c_r0bh]==""?0:$data[c_r0bh];
		   }else{
		   $tmp_hs=$data[c_r0ph]==""?0:$data[c_r0ph];
		   }
		   }

		   $sql="select * from \"Trs\" where \"Trs_pry\"='$cnospk_pilih' and \"Trs_a\"='$lokasi' and \"Trs_kode\"='$kode' and \"Trs_m\"='$c_m' and \"Trs_per\"='$periode' order by \"Trs_no\"";
		   $result=dbresult($sql);
		   if( pg_num_rows($result)!=0 ){
		   $tmp_rp1=0;
		   $tmp_vol1=0;
		   while( $data=pg_fetch_array($result) ){
		   $tmp_rp1+=($data[Trs_kurs]*$data[Trs_jumlah]);
		   $tmp_vol1+=$data[Trs_vol];
		   }
		   if( $tmp_vol1 != 0 ){
		   $tmp_hs=$tmp_rp1/$tmp_vol1;
		   }
		   }

		   $sql="select * from \"Perubahan\" where \"Ubh_pry\"='$cnospk_pilih' and \"Ubh_a\"='$lokasi' and \"Ubh_kode\"='$kode' and \"Ubh_m\"='$c_m' and \"Ubh_per\"='$periode'";
		   $result=dbresult($sql);
		   if( pg_num_rows($result) != 0 ){
		   $data=pg_fetch_array($result);
		   $tmp_hs=$data[Ubh_x]==""?0:$data[Ubh_x];
		   }
		 */


		return $tmp_hs;
	}

	function update_stscek_cc($sts_pry, $sts_a, $periode, $status=0){
		switch( $status ){
			case "1":
				$update=1;
			break;
			default:
			$update=0;
			break;
		}
		update_status_cc($sts_pry, $sts_a);
		$sql="update status set sts_cek='$update', sts_edit='$periode' where sts_pry='$sts_pry' and sts_a='$sts_a'";
		dbresult($sql);
	}
	function update_status_cc($sts_pry, $sts_a){
		$sts_pros=cari_nilai("select \"Rek_per\" from \"Rekap\" where \"Rek_pry\"='$sts_pry' and \"Rek_a\"='$sts_a' order by length(\"Rek_per\") desc, \"Rek_per\" desc limit 1");
		if( $sts_pros=="" ) {
			$sts_pros=cari_nilai("select \"R_per\" from \"Cost_real\" where \"R_pry\"='$sts_pry' and \"R_a\"='$sts_a' order by length(\"R_per\") desc, \"R_per\" desc limit 1")+1;
		}

		$sts_trs=cari_nilai("select \"Trs_per\" from \"Trs\" where \"Trs_pry\"='$sts_pry' and \"Trs_a\"='$sts_a' order by length(\"Trs_per\") desc, \"Trs_per\" desc limit 1");
		$sts_ubh=cari_nilai("select \"Ubh_per\" from \"Perubahan\" where \"Ubh_pry\"='$sts_pry' and \"Ubh_a\"='$sts_a' order by length(\"Ubh_per\") desc, \"Ubh_per\" desc limit 1");
		$sts_sbh=cari_nilai("select \"Eva_per\" from \"Evaluasi\" where \"Eva_pry\"='$sts_pry' and \"Eva_a\"='$sts_a' order by length(\"Eva_per\") desc, \"Eva_per\" desc limit 1");
		$sts_pros=$sts_pros==""?1:$sts_pros;

		$sts_trs=$sts_trs==""?1:$sts_trs;
		$sts_ubh=$sts_ubh==""?1:$sts_ubh;
		$sts_sbh=$sts_sbh==""?1:$sts_sbh;
		$sts_pros=empty($sts_pros)?1:$sts_pros;
		$sts_trs=empty($sts_trs)?1:$sts_trs;
		$sts_ubh=empty($sts_ubh)?1:$sts_ubh;
		$sts_sbh=empty($sts_sbh)?1:$sts_sbh;

		if( $sts_trs < $sts_pros ){
			$sts_trs=$sts_pros;
		}
		if( $sts_ubh < $sts_pros ){
			$sts_ubh=$sts_pros;
		}
		if( $sts_sbh < $sts_pros ){
			$sts_sbh=$sts_pros;
		}

		$sql="select * from status where sts_pry='$sts_pry' and sts_a='$sts_a'";
		$result=dbresult($sql);
		if( pg_num_rows($result)==0 ){
			$sql="insert into status values('$sts_pry', '$sts_a', '$sts_pros', '$sts_trs', '$sts_ubh', '$sts_sbh', '1', '', '$sts_pros')";
		}else{
			$sql="update status set sts_pros='$sts_pros', sts_trs='$sts_trs', sts_ubh='$sts_ubh', sts_sbh='$sts_sbh' where sts_pry='$sts_pry' and sts_a='$sts_a'";
		}
		dbresult($sql);
	}

	function hitung_akt_bln0($filter_kode, $no_proyek="", $divisi_kode="", $tahun=""){
		$find_pry=$no_proyek==""?"":"and kode_proyek='$no_proyek'";
		$find_divisi=$divisi_kode==""?"":"and data_divisi_kode='$divisi_kode'";
#Pengambilan perhitungan pada tabel tbl_summary_account
		$sql="select account_kode, ksll as kredit, dsll as debit
			from tbl_summary_account where true $find_pry $find_divisi and tahun='$tahun' and $filter_kode and substr(account_kode, 1, 1)::integer<4 and mu='RP'";
		$result=dbresult($sql);
#	echo "$sql <br />";
		while( $data=pg_fetch_array($result) ){
			$acc_jenis=cari_nilai("select acc_jenis from tbl_fn_account where acc_kode='$data[account_kode]'");
			$nilai=$acc_jenis==1?$data[debit]-$data[kredit]:$data[kredit]-$data[debit];
#		echo $acc_jenis." - ".$data[account_kode]." - ".$data[debit]." - ".$data[kredit];
			$hasil+=$nilai;
		}
#End
#Pengambilan perhitungan pada tabel tbl_summary_account2
		$sql="select account_kode, ksll as kredit, dsll as debit
			from tbl_summary_account2 where true $find_pry $find_divisi and tahun='$tahun' and $filter_kode and substr(account_kode, 1, 1)::integer>=4 and mu='RP'";
		$result=dbresult($sql);
#	echo "$sql <br />";
		while( $data=pg_fetch_array($result) ){
			$acc_jenis=cari_nilai("select acc_jenis from tbl_fn_account where acc_kode='$data[account_kode]'");
			$nilai=$acc_jenis==1?$data[debit]-$data[kredit]:$data[kredit]-$data[debit];
			$hasil+=$nilai;
		}
#End
		return ($hasil==""?0:$hasil);
	}

	function hitung_akt_perkiraan($filter_kode, $periode="", $no_proyek="", $divisi_kode="", $nbulan="", $ntahun=""){
		if( ! (empty($nbulan) && empty($ntahun)) ){
			$bulan=$nbulan;
			$tahun=$ntahun;
		}else{
			$bulan=ceil(substr($periode, 2, 2)/2);
			$tahun=date("Y", mktime(0,0,0,1,1,substr($periode, 0, 2)));
		}
		$find_pry=$no_proyek==""?"":"and kode_proyek='$no_proyek'";
		$find_divisi=$divisi_kode==""?"":"and data_divisi_kode='$divisi_kode'";
		for( $kali=1; $kali<=$bulan; $kali++ ){
			$arr_bln_kredit[]="kt_$kali";
			$arr_bln_debit[]="dt_$kali";
		}
		$fld_bln_kredit=implode("+", $arr_bln_kredit);
		$fld_bln_debit=implode("+", $arr_bln_debit);
#Pengambilan perhitungan pada tabel tbl_summary_account
		$sql="select 
			account_kode, 
			ksll+$fld_bln_kredit as kredit,
			dsll+$fld_bln_debit as debit
				from tbl_summary_account where true $find_pry $find_divisi and tahun='$tahun' and $filter_kode and substr(account_kode, 1, 1)::integer<4 and mu='RP'";
		$result=dbresult($sql);
#	echo "$sql <br />";
		while( $data=pg_fetch_array($result) ){
			$acc_jenis=cari_nilai("select acc_jenis from tbl_fn_account where acc_kode='$data[account_kode]'");
			$nilai=$acc_jenis==1?$data[debit]-$data[kredit]:$data[kredit]-$data[debit];
#		echo $acc_jenis." - ".$data[account_kode]." - ".$data[debit]." - ".$data[kredit];
			$hasil+=$nilai;
		}
#End
#Pengambilan perhitungan pada tabel tbl_summary_account2
		$sql="select 
			account_kode, 
			ksll+$fld_bln_kredit as kredit,
			dsll+$fld_bln_debit as debit
				from tbl_summary_account2 where true $find_pry $find_divisi and tahun='$tahun' and $filter_kode and substr(account_kode, 1, 1)::integer>=4 and mu='RP'";
		$result=dbresult($sql);
#	if( $no_proyek=="2005000029" ){
#		echo "$sql <br />";
#	}
		while( $data=pg_fetch_array($result) ){
			$acc_jenis=cari_nilai("select acc_jenis from tbl_fn_account where acc_kode='$data[account_kode]'");
#		if($data[account_kode]=='84111' || $data[account_kode]=='84121'){
#			echo "$data[account_kode] $acc_jenis debit:$data[debit] && kredit:$data[kredit]<br />";
#		}
			$nilai=$acc_jenis==1?$data[debit]-$data[kredit]:$data[kredit]-$data[debit];
			$hasil+=$nilai;
		}
#End
		return ($hasil==""?0:$hasil);
	}

	function buat_periode($tanggal){
		$arr_tgl=explode("-", $tanggal);
		$tahun=substr($arr_tgl[0], -2);
		$bulan=$arr_tgl[2]<16?($arr_tgl[1]*2)-1:$arr_tgl[1]*2;
		$bulan=$bulan<10?"0".$bulan:$bulan;
		$periode=$tahun.$bulan;

		return $periode;
	}

	function konversi_periode($periode){
		$bulan=ceil(substr($periode, -2)/2);
		$tahun=date("Y", mktime(0,0,0,$bulan, 1, substr($periode, 0, 2)));
		if( substr($periode, -2) % 2 == 0 ){
			$tgl[0]="$tahun-$bulan-16";
			$tgl[1]="$tahun-$bulan-".date("d", mktime(0, 0, 0, $bulan+1, 0, $tahun));
		}else{
			$tgl[0]="$tahun-$bulan-1";
			$tgl[1]="$tahun-$bulan-15";
		}
		return $tgl;
	}

	function nilai_rab_rap($no_spk){
		$tahap_kontrol_tanggal=cari_nilai("select tahap_kontrol_tanggal from tbl_tahap_kontrol where no_spk='$no_spk' order by tahap_kontrol_tanggal limit 1");
		if($tahap_kontrol_tanggal!=""){
			$kurs="select nilai_kurs from tbl_valas_kontrol where no_spk='$no_spk' and tahap_kontrol_tanggal='$tahap_kontrol_tanggal'";
			$sql_total="select * from tbl_tahap_kontrol where no_spk='$no_spk' and tahap_kontrol_tanggal='$tahap_kontrol_tanggal' and (tahap_kontrol_kode_induk is null or tahap_kontrol_kode_induk='')";
			$res_total=dbresult($sql_total);
			while($r_total=pg_fetch_array($res_total)){
				$total_rab+=$r_total[tahap_kontrol_total_rab]+($r_total[tahap_kontrol_valas_rab]*$kurs[nilai_kurs]);
				$total_rap+=$r_total[tahap_kontrol_total_rap]+$r_total[tahap_kontrol_valas_rap]*$kurs[nilai_kurs];
			}
		}
		$hasil=array("RAB"=>($total_rab/1000000),"RAP"=>($total_rap/1000000));
		return $hasil;
	}
	function replaced($message){
		$message	= trim($message);
		$string 	= $message;
		$string 	= strip_tags($string);
		$string 	= ereg_replace("\|", "", $string);
		$string 	= ereg_replace("\r", "", $string);
		$string 	= ereg_replace("\n", "<br />", $string);	
		$wordwrap = "25";
		$search	= "/([^\s]{".$wordwrap.",})/e";
		$replace= "''.wordwrap('\\1', ".$wordwrap.", ' ', 1).''";
		$string = preg_replace ($search, $replace, $string);	
		$string = str_replace("\"","",$string);	
		return $string;
	}
	function noBuktiKasBank($jenis="KI",$tahun,$cek_bukti=""){
		global $cdivisi_kode,$cnospk;
		switch($jenis){
			case "KO":
				$bkb_jenis="Pengeluaran Kas";
			break;
			case "KI":
				$bkb_jenis="Penerimaan Kas";
			break;
			case "BO":
				$bkb_jenis="Pengeluaran Bank";
			break;
			case "BI":
				$bkb_jenis="Penerimaan Bank";
			break;
		}
		if($cnospk=="0000000000"){
			$prefix=$jenis.$cdivisi_kode.substr($tahun,2,2);
		}else{
			$prefix=$jenis.$cnospk.substr($tahun,2,2);
		}
		$pjPrefix=strlen($prefix)+1;
		$sql="select max(substr(bkb_bukti,$pjPrefix,4)::numeric) as urutan from tbl_fn_buktikasbank where date_part('year',bkb_tanggal)='$tahun' and bkb_jenis='$bkb_jenis' and divisi_kode='$cdivisi_kode' and kode_cc='$cnospk'";
		$res=dbresult($sql);
		$r=pg_fetch_array($res);
		$urutan=$r[urutan]+1;
		$cekUrut = cari_nilai("select bkb_bukti from tbl_fn_buktikasbank where bkb_bukti = '".$prefix.str_pad($urutan,4,"0",STR_PAD_LEFT)."'");
		if ($cekUrut==""){
			$Urut = $urutan;
		} else {
			$Urut = $urutan + 1;
		}
		if($cek_bukti==""){
			$Urut = $Urut;
		}else{
			$Urut = $Urut +1;
		}
		return $prefix.str_pad($Urut,4,"0",STR_PAD_LEFT);
	} 
	function cekKunci($table,$field,$value,$message="",$check="",$other=""){
		$value=trim($value);
		if($check==""){
			$sql="select $field from $table where trim($field)='$value' $other";
		}else{
			$sql=$table.$other;
		}
		$res=dbresult($sql);
		if($r=pg_fetch_array($res)){
			if($message==""){
				$message=strtoupper($field)." SUDAH ADA";
			}
			echo "<script>alert('".$message."');";
			echo "window.history.back();</script>";
			die('Please Enable Javascript');
		}
	}

?>
